Learning Selenium WebDriver

Inject the Sizzle CSS selector library

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"));
}
}
This entry was posted in Tips & Tricks on February 4, 2012 by Roy de Kleijn.




原文地址:http://selenium.polteq.com/injecting-the-sizzle-css-selector-library/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值