Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port of protractor locators to new WebDriver JS locator style
- Loading branch information
0 parents
commit 10418a0
Showing
11 changed files
with
606 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
target | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
The MIT License | ||
|
||
Portions Copyright (c) 2010-2013 Google, Inc. | ||
Portions Copyright (c) 2013 Paul Hammant | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<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.paulhammant</groupId> | ||
<artifactId>ngwebdriver</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>ngWebDriver</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.seleniumhq.selenium</groupId> | ||
<artifactId>selenium-api</artifactId> | ||
<version>2.0-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<version>6.8.5</version> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<version>1.3</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.seleniumhq.selenium</groupId> | ||
<artifactId>selenium-firefox-driver</artifactId> | ||
<version>2.0-SNAPSHOT</version> | ||
<!-- version>[2.36,)</version --> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/paulhammant/ngwebdriver/AngularModelAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.paulhammant.ngwebdriver; | ||
|
||
import org.openqa.selenium.JavascriptExecutor; | ||
import org.openqa.selenium.WebElement; | ||
|
||
public class AngularModelAccessor { | ||
|
||
|
||
private JavascriptExecutor driver; | ||
|
||
public AngularModelAccessor(JavascriptExecutor driver) { | ||
this.driver = driver; | ||
} | ||
|
||
public void mutate(WebElement element, final String variable, final String value) { | ||
|
||
driver.executeScript("angular.element(arguments[0]).scope()." + variable + " = " + value + ";" + | ||
"angular.element(document.body).injector().get('$rootScope').$apply();", element); | ||
|
||
|
||
} | ||
|
||
public String retrieveJson(WebElement element, final String variable) { | ||
|
||
return (String) driver.executeScript("return angular.toJson(angular.element(arguments[0]).scope()." + variable + ");", element); | ||
|
||
} | ||
|
||
public Object retrieve(WebElement element, final String variable) { | ||
return driver.executeScript("return angular.element(arguments[0]).scope()." + variable + ";", element); | ||
} | ||
|
||
public String retrieveAsString(WebElement element, final String variable) { | ||
return (String) driver.executeScript("return angular.element(arguments[0]).scope()." + variable + ";", element); | ||
} | ||
|
||
public Long retrieveAsLong(WebElement element, final String variable) { | ||
return (Long) driver.executeScript("return angular.element(arguments[0]).scope()." + variable + ";", element); | ||
} | ||
|
||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/paulhammant/ngwebdriver/ByAngularBinding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.paulhammant.ngwebdriver; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.SearchContext; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import java.util.List; | ||
|
||
public class ByAngularBinding extends By { | ||
|
||
public static ByAngularBinding angularBinding(String binding) { | ||
return new ByAngularBinding(binding); | ||
} | ||
|
||
private String binding; | ||
private int row = 1; | ||
|
||
public ByAngularBinding(String binding) { | ||
this.binding = binding; | ||
} | ||
|
||
private By makeJsBy(String oneOrAll) { | ||
return By.js( | ||
"var using = arguments[0] || document;\n" + | ||
"var binding = '" + binding + "';\n" + | ||
"var bindings = using.getElementsByClassName('ng-binding');\n" + | ||
"var matches = [];\n" + | ||
"for (var i = 0; i < bindings.length; ++i) {\n" + | ||
" var bindingName = angular.element(bindings[i]).data().$binding[0].exp ||\n" + | ||
" angular.element(bindings[i]).data().$binding;\n" + | ||
" if (bindingName.indexOf(binding) != -1) {\n" + | ||
" matches.push(bindings[i]);\n" + | ||
" }\n" + | ||
"}\n" + | ||
"return matches" + oneOrAll + ";" | ||
); | ||
} | ||
|
||
@Override | ||
public WebElement findElement(SearchContext context) { | ||
return makeJsBy("[0]").findElement(context); | ||
} | ||
|
||
@Override | ||
public List<WebElement> findElements(SearchContext searchContext) { | ||
return makeJsBy("").findElements(searchContext); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/paulhammant/ngwebdriver/ByAngularRepeater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.paulhammant.ngwebdriver; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.SearchContext; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import java.util.List; | ||
|
||
public class ByAngularRepeater extends By { | ||
|
||
public static ByAngularRepeater angularRepeater(String repeater) { | ||
return new ByAngularRepeater(repeater); | ||
} | ||
|
||
private String repeater; | ||
|
||
public ByAngularRepeater(String repeater) { | ||
this.repeater = repeater; | ||
} | ||
|
||
private By makeJsBy() { | ||
return By.js( | ||
"var using = arguments[0] || document;\n" + | ||
"var repeater = '" + repeater + "';\n" + | ||
"\n" + | ||
"var rows = [];\n" + | ||
"var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-'];\n" + | ||
"for (var p = 0; p < prefixes.length; ++p) {\n" + | ||
" var attr = prefixes[p] + 'repeat';\n" + | ||
" var repeatElems = using.querySelectorAll('[' + attr + ']');\n" + | ||
" attr = attr.replace(/\\\\/g, '');\n" + | ||
" for (var i = 0; i < repeatElems.length; ++i) {\n" + | ||
" if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) {\n" + | ||
" rows.push(repeatElems[i]);\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}\n" + | ||
"return rows;"); | ||
} | ||
|
||
public ByAngularRepeaterRow row(int row) { | ||
return new ByAngularRepeaterRow(repeater, row); | ||
} | ||
|
||
public ByAngularRepeaterColumn column(String column) { | ||
return new ByAngularRepeaterColumn(repeater, column); | ||
} | ||
|
||
@Override | ||
public WebElement findElement(SearchContext context) { | ||
return makeJsBy().findElement(context); | ||
} | ||
|
||
@Override | ||
public List<WebElement> findElements(SearchContext searchContext) { | ||
return makeJsBy().findElements(searchContext); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/paulhammant/ngwebdriver/ByAngularRepeaterCell.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.paulhammant.ngwebdriver; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.SearchContext; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import java.util.List; | ||
|
||
public class ByAngularRepeaterCell extends By { | ||
|
||
private final String repeater; | ||
private final int row; | ||
private final String column; | ||
|
||
public ByAngularRepeaterCell(String repeater, int row, String column) { | ||
this.repeater = repeater; | ||
this.row = row; | ||
this.column = column; | ||
} | ||
|
||
private By makeJsBy() { | ||
return By.js( | ||
"var matches = [];\n" + | ||
"var using = arguments[0] || document;\n" + | ||
"var repeater = '" + repeater + "';\n" + | ||
"var index = " + row + ";\n" + | ||
"var binding = '" + column + "';\n" + | ||
"var rows = [];\n" + | ||
"var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\\\:'];\n" + | ||
"for (var p = 0; p < prefixes.length; ++p) {\n" + | ||
" var attr = prefixes[p] + 'repeat';\n" + | ||
" var repeatElems = using.querySelectorAll('[' + attr + ']');\n" + | ||
" attr = attr.replace(/\\\\/g, '');\n" + | ||
" for (var i = 0; i < repeatElems.length; ++i) {\n" + | ||
" if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) {\n" + | ||
" rows.push(repeatElems[i]);\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}\n" + | ||
"var row = rows[index - 1];\n" + | ||
"var bindings = [];\n" + | ||
"if (row.className.indexOf('ng-binding') != -1) {\n" + | ||
" bindings.push(row);\n" + | ||
"}\n" + | ||
"var childBindings = row.getElementsByClassName('ng-binding');\n" + | ||
"for (var i = 0; i < childBindings.length; ++i) {\n" + | ||
" bindings.push(childBindings[i]);\n" + | ||
"}\n" + | ||
"for (var i = 0; i < bindings.length; ++i) {\n" + | ||
" var bindingName = angular.element(bindings[i]).data().$binding[0].exp ||\n" + | ||
" angular.element(bindings[i]).data().$binding;\n" + | ||
" if (bindingName.indexOf(binding) != -1) {\n" + | ||
" matches.push(bindings[i]);\n" + | ||
" }\n" + | ||
"}\n" + | ||
"// We can only return one with webdriver.findElement.\n" + | ||
"return matches[0];" | ||
); | ||
} | ||
|
||
@Override | ||
public WebElement findElement(SearchContext context) { | ||
return makeJsBy().findElement(context); | ||
} | ||
|
||
// meaningless | ||
@Override | ||
public List<WebElement> findElements(SearchContext searchContext) { | ||
throw new UnsupportedOperationException("This locator zooms in on a single row, findElements() is meaningless"); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/paulhammant/ngwebdriver/ByAngularRepeaterColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.paulhammant.ngwebdriver; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.SearchContext; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import java.util.List; | ||
|
||
public class ByAngularRepeaterColumn extends By { | ||
|
||
private final String repeater; | ||
private final String column; | ||
|
||
public ByAngularRepeaterColumn(String repeater, String column) { | ||
this.repeater = repeater; | ||
this.column = column; | ||
} | ||
|
||
private By makeJsBy() { | ||
return By.js( | ||
"var matches = [];\n" + | ||
"var using = arguments[0] || document;\n" + | ||
"var repeater = '" + repeater + "';\n" + | ||
"var binding = '" + column + "';\n" + | ||
"\n" + | ||
"var rows = [];\n" + | ||
"var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-'];\n" + | ||
"for (var p = 0; p < prefixes.length; ++p) {\n" + | ||
" var attr = prefixes[p] + 'repeat';\n" + | ||
" var repeatElems = using.querySelectorAll('[' + attr + ']');\n" + | ||
" attr = attr.replace(/\\\\/g, '');\n" + | ||
" for (var i = 0; i < repeatElems.length; ++i) {\n" + | ||
" if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) {\n" + | ||
" rows.push(repeatElems[i]);\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}\n" + | ||
"for (var i = 0; i < rows.length; ++i) {\n" + | ||
" var bindings = [];\n" + | ||
" if (rows[i].className.indexOf('ng-binding') != -1) {\n" + | ||
" bindings.push(rows[i]);\n" + | ||
" }\n" + | ||
" var childBindings = rows[i].getElementsByClassName('ng-binding');\n" + | ||
" for (var k = 0; k < childBindings.length; ++k) {\n" + | ||
" bindings.push(childBindings[k]);\n" + | ||
" }\n" + | ||
" for (var j = 0; j < bindings.length; ++j) {\n" + | ||
" var bindingName = angular.element(bindings[j]).data().$binding[0].exp ||\n" + | ||
" angular.element(bindings[j]).data().$binding;\n" + | ||
" if (bindingName.indexOf(binding) != -1) {\n" + | ||
" matches.push(bindings[j]);\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}\n" + | ||
"return matches;" | ||
); | ||
} | ||
|
||
// meaningless | ||
@Override | ||
public WebElement findElement(SearchContext context) { | ||
throw new UnsupportedOperationException("This locator zooms in on a multiple cells, findElement() is meaningless"); | ||
} | ||
|
||
@Override | ||
public List<WebElement> findElements(SearchContext searchContext) { | ||
return makeJsBy().findElements(searchContext); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.