Use square brackets to
further concentrate the search
The @ symbol facilitates working with attributes.
//book[@type=”hardback”]
The first book element is read from the XML document using the following XPath:
/books/book[1]
The @ symbol facilitates working with attributes.
//book[@type=”hardback”]
The first book element is read from the XML document using the following XPath:
/books/book[1]
A double slash (//)
signals that all elements in the XML document that match the search criteria
are returned, regardless of location/level within the document.
Use a double slash for location-agnostic element matching
Use a double slash for location-agnostic element matching
The slash (/)
separates child nodes
Search for specific nestings anywhere in a document
The following XPath statement returns all book elements:
//books/book
<?xml version="1.0" encoding="ISO-8859-1"?>
<books>
<book type=”hardback”>
<title>Atlas Shrugged</title>
<author>Ayn Rand</author>
<isbn>0525934189</isbn>
</book>
<book type=”paperback”>
<title>A Burnt-Out Case</title>
<author>Graham Greene</author>
<isbn>0140185399</isbn>
</book>
</books>
Search for specific nestings anywhere in a document
The following XPath statement returns all book elements:
//books/book
<?xml version="1.0" encoding="ISO-8859-1"?>
<books>
<book type=”hardback”>
<title>Atlas Shrugged</title>
<author>Ayn Rand</author>
<isbn>0525934189</isbn>
</book>
<book type=”paperback”>
<title>A Burnt-Out Case</title>
<author>Graham Greene</author>
<isbn>0140185399</isbn>
</book>
</books>
By.xpath("//a[contains(@href,
'login')]")
Posted 27th April 2012 by mayu kataoka
WebDriverWait wait = new
WebDriverWait(_driver, TimeSpan.FromSeconds(4));
WebElement autocomplete
= wait.Until(x => x.FindElement(By.ClassName("xyz")))
http://deanhume.com/Home/BlogPost/selenium-webdriver---wait-for-an-element-to-load/64
<select id="dobYear" value="" name="dobYear">
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
Select select = new Select(driver.findElement(By.tagName("select")));
select.deselectAll();
select.selectByVisibleText("2008");
Instead of
"driver.findElement(By.id("submit")).click();",
you can use a simpler form "element.submit();"
you can use a simpler form "element.submit();"
driver.navigate().to("http://www.example.com");
driver.navigate().forward();
driver.navigate().back();
// By name
driver.manage().deleteCookieNamed("CookieName");
// By Cookie
driver.manage().deleteCookie(loadedCookie);
// Or all of them
driver.manage().deleteAllCookies();
// And now output all the available cookies for the current URL
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
}
// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");
List<WebElement> labels = driver.findElements(By.tagName("label"));
List<WebElement> inputs = (List<WebElement>) ((JavascriptExecutor)driver).executeScript(
"var labels = arguments[0], inputs = []; for
(var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute('for')));
} return inputs;", labels);
Target HTML
<div id="food"><span class="dairy">milk</span><span
class="dairy aged">cheese</span></div>
CSS expression
WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));
http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
import org.openqa.selenium.support.ui.WebDriverWait;
10 means time out after 10 seconds.
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
http://code.google.com/p/selenium/wiki/XpathInWebDriver
See the code at the
bottom of
http://thomassundberg.wordpress.com/2011/10/18/testing-a-web-application-with-selenium-2/#more-615
http://thomassundberg.wordpress.com/2011/10/18/testing-a-web-application-with-selenium-2/#more-615
http://thomassundberg.wordpress.com/2011/10/18/testing-a-web-application-with-selenium-2/#more-615
Some page object examples here:
https://github.com/Ardesco/Ebselen/tree/master/ebselen-tests/src/test/java/c
om/lazerycode/ebselen/pagefactory/google
Used by the tests here:
https://github.com/Ardesco/Ebselen/tree/master/ebselen-tests/src/test/java/c
om/lazerycode/ebselen/website/google
Very simplistic but should hopefully be a decent pointer.
Some page object examples here:
https://github.com/Ardesco/Ebselen/tree/master/ebselen-tests/src/test/java/c
om/lazerycode/ebselen/pagefactory/google
Used by the tests here:
https://github.com/Ardesco/Ebselen/tree/master/ebselen-tests/src/test/java/c
om/lazerycode/ebselen/website/google
Very simplistic but should hopefully be a decent pointer.
WebDriverWait myWait = new
WebDriverWait(webDriver, 45);
ExpectedCondition<Boolean> conditionToCheck = new
ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver input) {
return (input.findElements(By.id("fContent").size()
> 0);
}
};
myWait.until(conditionToCheck);
create two packages
package AAA contains some test classes
(Import class BBB)
package AAA contains some test classes
(Import class BBB)
package BBB contains some Page Object classes
- Page objects
themselves should never be make verifications or assertions.
-There is one, single, verification which can, and should, be within the page object and that is to verify that the page, and possibly critical elements on the page, were loaded correctly.
-There is one, single, verification which can, and should, be within the page object and that is to verify that the page, and possibly critical elements on the page, were loaded correctly.
/**
* Page Object encapsulates the Sign-in page.
*/
public class SignInPage
{
private Selenium selenium;
public SignInPage(Selenium selenium) {
this.selenium = selenium;
if(!selenium.getTitle().equals("Sign
in page")) {
throw new IllegalStateException("This
is not sign in page, current page is: " +selenium.getLocation());
}
}
/**
* Login as valid user
*
* @param userName
* @param password
* @return HomePage object
*/
public HomePage loginValidUser(String userName,
String password) {
selenium.type("usernamefield",
userName);
selenium.type("passwordfield",
password);
selenium.click("sign-in");
selenium.waitForPageToLoad("waitPeriod");
return new HomePage(selenium);
}
}
------------------------------------------------
/**
* Page Object encapsulates the Home Page
*/
public class HomePage {
private Selenium selenium;
public HomePage(Selenium selenium) {
if (!selenium.getTitle().equals("Home
Page of logged in user")) {
throw new IllegalStateException("This
is not Home Page of logged in user, current page" + "is: " +selenium.getLocation());
}
}
public HomePage manageProfile() {
// Page encapsulation to manage
profile functionality
return new HomePage(selenium);
}
/*More methods offering the services
represented by Home Page
of Logged User. These methods in turn
might return more Page Objects
for example click on Compose mail
button could return ComposeMail class object*/
}
---------------------------------------------------
/***
* Tests login feature
*/
public class TestLogin
{
public void testLogin() {
SignInPage signInPage = new SignInPage(selenium);
HomePage homePage = signInPage.loginValidUser("userName",
"password");
Assert.assertTrue(selenium.isElementPresent("compose
button"),"Login was unsuccessful");
}
}
The footer section
HTML is : <div id = "footer"> .......
</div>
driver.findElements(By.xpath("//div[@id='footer']/descendant::a"));
driver.findElements(By.xpath("//div[@id='footer']/descendant::a"));
* Target html
is //html/body/div/div[2]
* div[2] changes to div[3] or div[4] etc dynamically
Xpath -- driver.findElements(By.xpath("//html/body/div/")).findElement(By.tagName("div"));
* div[2] changes to div[3] or div[4] etc dynamically
Xpath -- driver.findElements(By.xpath("//html/body/div/")).findElement(By.tagName("div"));
Target element:
<input type="email" placeholder="Email" value="" style="display:
block;" name="emailAccountID" id="emailAccountID">
How to execute Javascript:
((JavascriptExecutor) driver).executeScript(@"this.browserbot.getUserWindow().document.getElementById("emailAccountID").style.visibility="visible";");
<input type="email" placeholder="Email" value="" style="display:
block;" name="emailAccountID" id="emailAccountID">
How to execute Javascript:
((JavascriptExecutor) driver).executeScript(@"this.browserbot.getUserWindow().document.getElementById("emailAccountID").style.visibility="visible";");
http://code.google.com/p/selenium/w/list
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override", "some
UA string");
WebDriver driver = new FirefoxDriver(profile);
http://code.google.com/p/selenium/wiki/PageFactory
http://code.google.com/p/selenium/wiki/PageObjects
http://code.google.com/p/selenium/wiki/LoadableComponent
http://code.google.com/p/selenium/wiki/PageObjects
http://code.google.com/p/selenium/wiki/LoadableComponent