Skip to content
Permalink
Browse files

Adding initial files

  • Loading branch information
sukgu committed Mar 2, 2019
1 parent 9c0ec1f commit a6cc6d9f6650fe8c8da274d0a4e41bc2d9be5e3c
@@ -10,3 +10,31 @@ buildNumber.properties

# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
!/.mvn/wrapper/maven-wrapper.jar

*target*
*.jar
*.war
*.ear
*.class

# eclipse specific git ignore
*.pydevproject
.project
.metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch
68 pom.xml
@@ -0,0 +1,68 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.shadow</groupId>
<artifactId>automation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>automation</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.14.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.14.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>

</dependencies>
</project>
@@ -0,0 +1,135 @@
var querySelectorAllDeep = function querySelectorAllDeep(selector, root = document) {
return _querySelectorDeep(selector, true, root);
};

var querySelectorDeep = function querySelectorDeep(selector, root = document) {
return _querySelectorDeep(selector, false, root);
};

var getObject = function getObject(selector, root = document) {
const multiLevelSelectors = splitByCharacterUnlessQuoted(selector, '>');
if (multiLevelSelectors.length == 1) {
return querySelectorDeep(multiLevelSelectors[0], root);
} else if (multiLevelSelectors.length == 2) {
return querySelectorDeep(multiLevelSelectors[1], querySelectorDeep(multiLevelSelectors[0]).root);
} else if (multiLevelSelectors.length == 3) {
return querySelectorDeep(multiLevelSelectors[2], querySelectorDeep(multiLevelSelectors[1], querySelectorDeep(multiLevelSelectors[0]).root));
}

};

var getAllObject = function getAllObject(selector, root = document) {
const multiLevelSelectors = splitByCharacterUnlessQuoted(selector, '>');
if (multiLevelSelectors.length == 1) {
return querySelectorAllDeep(multiLevelSelectors[0], root);
} else if (multiLevelSelectors.length == 2) {
return querySelectorAllDeep(multiLevelSelectors[1], querySelectorDeep(multiLevelSelectors[0]).root);
} else if (multiLevelSelectors.length == 3) {
return querySelectorAllDeep(multiLevelSelectors[2], querySelectorDeep(multiLevelSelectors[1], querySelectorDeep(multiLevelSelectors[0]).root));
}

};

function _querySelectorDeep(selector, findMany, root) {
let lightElement = root.querySelector(selector);

if (document.head.createShadowRoot || document.head.attachShadow) {
if (!findMany && lightElement) {
return lightElement;
}

const selectionsToMake = splitByCharacterUnlessQuoted(selector, ',');

return selectionsToMake.reduce((acc, minimalSelector) => {
if (!findMany && acc) {
return acc;
}
const splitSelector = splitByCharacterUnlessQuoted(minimalSelector
.replace(/^\s+/g, '')
.replace(/\s*([>+~]+)\s*/g, '$1'), ' ')
.filter((entry) => !!entry);
const possibleElementsIndex = splitSelector.length - 1;
const possibleElements = collectAllElementsDeep(splitSelector[possibleElementsIndex], root);
const findElements = findMatchingElement(splitSelector, possibleElementsIndex, root);
if (findMany) {
acc = acc.concat(possibleElements.filter(findElements));
return acc;
} else {
acc = possibleElements.find(findElements);
return acc;
}
}, findMany ? [] : null);


} else {
if (!findMany) {
return lightElement;
} else {
return root.querySelectorAll(selector);
}
}

}

function findMatchingElement(splitSelector, possibleElementsIndex, root) {
return (element) => {
let position = possibleElementsIndex;
let parent = element;
let foundElement = false;
while (parent) {
const foundMatch = parent.matches(splitSelector[position]);
if (foundMatch && position === 0) {
foundElement = true;
break;
}
if (foundMatch) {
position--;
}
parent = findParentOrHost(parent, root);
}
return foundElement;
};

}

function splitByCharacterUnlessQuoted(selector, character) {
return selector.match(/\\?.|^$/g).reduce((p, c) => {
if (c === '"' && !p.sQuote) {
p.quote ^= 1;
p.a[p.a.length - 1] += c;
} else if (c === '\'' && !p.quote) {
p.sQuote ^= 1;
p.a[p.a.length - 1] += c;

} else if (!p.quote && !p.sQuote && c === character) {
p.a.push('');
} else {
p.a[p.a.length - 1] += c;
}
return p;
}, { a: [''] }).a;
}


function findParentOrHost(element, root) {
const parentNode = element.parentNode;
return (parentNode && parentNode.host && parentNode.nodeType === 11) ? parentNode.host : parentNode === root ? null : parentNode;
}


function collectAllElementsDeep(selector = null, root) {
const allElements = [];

const findAllElements = function(nodes) {
for (let i = 0, el; el = nodes[i]; ++i) {
allElements.push(el);
if (el.shadowRoot) {
findAllElements(el.shadowRoot.querySelectorAll('*'));
}
}
};

findAllElements(root.querySelectorAll('*'));

return selector ? allElements.filter(el => el.matches(selector)) : allElements;
}
@@ -0,0 +1,155 @@
package com.shadow.automation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Shadow {

WebDriver driver;
WebDriverException exception;
WebDriverWait wait;
CommandExecutor executer;
SessionId sessionId;
ChromeDriver chromeDriver;
FirefoxDriver firfoxDriver;
InternetExplorerDriver ieDriver;

public Shadow(WebDriver driver) {

if (driver instanceof ChromeDriver) {
sessionId = ((ChromeDriver)driver).getSessionId();
chromeDriver = (ChromeDriver)driver;
} else if (driver instanceof FirefoxDriver) {
sessionId = ((FirefoxDriver)driver).getSessionId();
firfoxDriver = (FirefoxDriver)driver;
} else if (driver instanceof InternetExplorerDriver) {
sessionId = ((InternetExplorerDriver)driver).getSessionId();
ieDriver = (InternetExplorerDriver)driver;
}
this.driver = driver;
}

private Object injectShadowExecuter(String javascript) {
if(chromeDriver!=null) {
JavascriptExecutor js = (JavascriptExecutor)chromeDriver;
waitForPageLoaded();
return js.executeScript(javascript);
} else if (firfoxDriver!=null) {
waitForPageLoaded();
return firfoxDriver.executeScript(javascript);
} else if (ieDriver!=null) {
waitForPageLoaded();
return ieDriver.executeScript(javascript);
} else {
return null;
}
}

private Object executerGetObject(String script) {
String javascript = convertJStoText().toString();
javascript += script;
return injectShadowExecuter(javascript);
}

private StringBuilder convertJStoText() {
File jsFile = new File("resource/querySelector.js");
BufferedReader reader = null;
StringBuilder text = new StringBuilder();
try {
reader = new BufferedReader(new FileReader(jsFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(reader!=null) {
try {
while(reader.ready()) {
text.append(reader.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(reader!=null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return text;
}

private void fixLocator(SearchContext context, String cssLocator, WebElement element) {
if (element instanceof RemoteWebElement) {
try {
@SuppressWarnings("rawtypes")
Class[] parameterTypes = new Class[] { SearchContext.class,
String.class, String.class };
Method m = element.getClass().getDeclaredMethod(
"setFoundBy", parameterTypes);
m.setAccessible(true);
Object[] parameters = new Object[] { context, "cssSelector", cssLocator };
m.invoke(element, parameters);
} catch (Exception fail) {
//fail("Something bad happened when fixing locator");
}
}
}

private void waitForPageLoaded() {
ExpectedCondition<Boolean> expectation = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete");
}
};
try {
Thread.sleep(1000);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(expectation);
} catch (Throwable error) {
Assertions.fail("Timeout waiting for Page Load Request to complete.");
}
}

public WebElement findElement(String cssSelector) {
WebElement element = null;
element = (WebElement) executerGetObject("return getObject(\""+cssSelector+"\");");
fixLocator(driver, cssSelector, element);
return element;
}

@SuppressWarnings("unchecked")
public List<WebElement> findElements(String cssSelector) {
List<WebElement> element = null;
Object object = executerGetObject("return getAllObject(\""+cssSelector+"\");");
if(object!=null && object instanceof List<?>) {
element = (List<WebElement>) object;
}
for (WebElement webElement : element) {
fixLocator(driver, cssSelector, webElement);
}
return element;
}

}

0 comments on commit a6cc6d9

Please sign in to comment.
You can’t perform that action at this time.