Problem
We might experience some locator issues during the transition from Selenium 1 to Selenium WebDriver. We face these issues because Selenium WebDriver stays strict to the CSS standard. It might that :contains(‘text’) does not locate the web page elements anymore. The solution is to inject the Sizzle engine into the browser under test. We can make a SizzleSelector class and use the power of Sizzleselector where necessary. Shannon Code came up with a very nice class, which can be found here: http://www.prototypic.net/weblog/?p=14
Solution
This is the code of the SizzleSelector class. It injects the Sizzle engine into the browser under test by JavaScript.
import java.util.List;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
@SuppressWarnings("unchecked")
public class SizzleSelector {
private JavascriptExecutor driver;
public SizzleSelector(WebDriver webDriver) {
driver = (JavascriptExecutor) webDriver;
}
public WebElement findElementBySizzleCss(String using) {
injectSizzleIfNeeded();
String javascriptExpression = createSizzleSelectorExpression(using);
List<WebElement> elements = (List<WebElement>) driver
.executeScript(javascriptExpression);
if (elements.size() > 0)
return (WebElement) elements.get(0);
return null;
}
public List<WebElement> findElementsBySizzleCss(String using) {
injectSizzleIfNeeded();
String javascriptExpression = createSizzleSelectorExpression(using);
return (List<WebElement>) driver.executeScript(javascriptExpression);
}
private String createSizzleSelectorExpression(String using) {
return "return Sizzle(\"" + using + "\")";
}
private void injectSizzleIfNeeded() {
if (!sizzleLoaded())
injectSizzle();
}
public Boolean sizzleLoaded() {
Boolean loaded;
try {
loaded = (Boolean) driver.executeScript("return Sizzle()!=null");
} catch (WebDriverException e) {
loaded = false;
}
return loaded;
}
public void injectSizzle() {
driver.executeScript(" var headID = document.getElementsByTagName(\"head\")[0];"
+ "var newScript = document.createElement('script');"
+ "newScript.type = 'text/javascript';"
+ "newScript.src = 'https://raw.github.com/jquery/sizzle/master/sizzle.js';"
+ "headID.appendChild(newScript);");
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.List;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
@SuppressWarnings("unchecked")
public class SizzleSelector {
private JavascriptExecutor driver;
public SizzleSelector(WebDriver webDriver) {
driver = (JavascriptExecutor) webDriver;
}
public WebElement findElementBySizzleCss(String using) {
injectSizzleIfNeeded();
String javascriptExpression = createSizzleSelectorExpression(using);
List<WebElement> elements = (List<WebElement>) driver
.executeScript(javascriptExpression);
if (elements.size() > 0)
return (WebElement) elements.get(0);
return null;
}
public List<WebElement> findElementsBySizzleCss(String using) {
injectSizzleIfNeeded();
String javascriptExpression = createSizzleSelectorExpression(using);
return (List<WebElement>) driver.executeScript(javascriptExpression);
}
private String createSizzleSelectorExpression(String using) {
return "return Sizzle(\"" + using + "\")";
}
private void injectSizzleIfNeeded() {
if (!sizzleLoaded())
injectSizzle();
}
public Boolean sizzleLoaded() {
Boolean loaded;
try {
loaded = (Boolean) driver.executeScript("return Sizzle()!=null");
} catch (WebDriverException e) {
loaded = false;
}
return loaded;
}
public void injectSizzle() {
driver.executeScript(" var headID = document.getElementsByTagName(\"head\")[0];"
+ "var newScript = document.createElement('script');"
+ "newScript.type = 'text/javascript';"
+ "newScript.src = 'https://raw.github.com/jquery/sizzle/master/sizzle.js';"
+ "headID.appendChild(newScript);");
}
}
|
How to use it
The class described above will inject the Sizzle engine into the browser under test. This allows us to use all the Sizzle selector features, such as :contains(‘text’). Other classes can extended this class and we are able to use the findElementBySizzleCss() method to locate the elements.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class SizzleExample {
private static WebDriver driver;
SizzleSelector sizzle;
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
sizzle = new SizzleSelector(driver);
driver.get("http://selenium.polteq.com/prestashop/");
}
@AfterClass
public void tearDown() {
driver.close();
driver.quit();
}
@Test
public void useSizzleSelector() {
sizzle.findElementBySizzleCss("input#search_query_top").sendKeys("ipod nano");
sizzle.findElementBySizzleCss("input[name='submit_search']").click();
String searchHeader = sizzle.findElementBySizzleCss("H1").getText().toLowerCase();
Assert.assertTrue(searchHeader.contains("ipod nano"));
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class SizzleExample {
private static WebDriver driver;
SizzleSelector sizzle;
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
sizzle = new SizzleSelector(driver);
driver.get("http://selenium.polteq.com/prestashop/");
}
@AfterClass
public void tearDown() {
driver.close();
driver.quit();
}
@Test
public void useSizzleSelector() {
sizzle.findElementBySizzleCss("input#search_query_top").sendKeys("ipod nano");
sizzle.findElementBySizzleCss("input[name='submit_search']").click();
String searchHeader = sizzle.findElementBySizzleCss("H1").getText().toLowerCase();
Assert.assertTrue(searchHeader.contains("ipod nano"));
}
}
|