Hello Friends,
Hope you have now familiar with Web-driver and and how to automate a web page. Now will see some important methods in webdriver.
#implicit wait - it will wait for 10sec
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
#explicit wait
1. Here cond return true
Type 1:
WebElement elem= new WebDriverWait(driver, 10).until(visibilityOfElementLocated(By.id("id_of_web_element")));
WebElement elem= new WebDriverWait(driver, 10).until(visibilityOfElementLocated(By.className("class_of_element")));
Type 2
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return driver.findElement(By.id( "myDynamicElement" ));
}});
Type 3
(new WebDriverWait(driverS, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("Letsbuy");
}
});
#By ID
WebElement element=driver.FindElement(By.id(BuyNowButton));
#By Class Name
List<WebElements> buttons=driver.FindElements(By.className("buttons"));
#By Tag Name
WebElement iframe=driver.FindElement(By.tagName("iframe"));
#By Name
WebElement name=driver.FindElement(By.name("firstName"));
#By LinkText
<a href="www.google.com/selenium.php">open-selenium</a>
WebElement link=driver.FindElement(By.linkText("open-selenium"));
#By Partial Link Text
<a href="www.google.com/selenium_setup.php">open selenium webdriver setup</a>
WebElement link=driver.FindElement(By.partialLinkText("setup"));
#By CSS [NEED MORE INFO ???]
-->CSS may vary from browser to browser hence not prefered.
-->browser native support for CSS will be used and if brwoser not supporting CSS then "Sizzle" will be used. eg IE 6,7 and FF 3.0 use Sizzle as search engine.
<div id= "food" ><span class= "dairy" >milk</span><span class= "dairy aged" >cheese</span>
WebElement cheese = driver.findElement(By.cssSelector( "#food span.dairy.aged" ));
#By Xpath
WebElement element = driver.findElements(By.xpath("//div[contains(text(),'Click Payment History')]"))
WebElement element = driver.findElements(By.xpath("//div[@class=\"articleHeader\"])).get(2);
#To get Hidden field
//input[@type="hidden"][@id="FinalPrice"]/@value
//input[@id="FinalPrice"]@value
#Relative XPath:
<table id="serviceInvocations" width="100%" style="display: block; height: 250px; overflow: auto;">
A: ".//*[@id='serviceInvocations']/tbody/tr[3]/td[1]"
#Xpath Functions:
//div[@id='summaryTip']//div[@class='xpop-content'][text()='You will see a summary of your transfer as you proceed. ']
driver.findElement(By.xpath(".//*[contains(text(),'Sign out')]")).click();
Few More - Xpath:
1. Verify button visibilty [class='select'] get appended if button is enable
String xpBrands = "//ul[@id='views']/li[@class='selected' and position()=1]/div/p/a";
2. Verify count of similar element
String xpTopScrollLinks = "//div[@id='topscrolllinks']/div/a";
int topScrollLinksCount = driver.findElements(By.xpath(xpTopScrollLinks)).size();
//div[@class='promote_favorite']/div[@id='fav_brand_775321 and @class='favorite faved']
Regards
Sheetal :)
Hope you have now familiar with Web-driver and and how to automate a web page. Now will see some important methods in webdriver.
#implicit wait - it will wait for 10sec
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
#explicit wait
1. Here cond return true
Type 1:
WebElement elem= new WebDriverWait(driver, 10).until(visibilityOfElementLocated(By.id("id_of_web_element")));
WebElement elem= new WebDriverWait(driver, 10).until(visibilityOfElementLocated(By.className("class_of_element")));
Type 2
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return driver.findElement(By.id( "myDynamicElement" ));
}});
Type 3
(new WebDriverWait(driverS, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("Letsbuy");
}
});
#By ID
WebElement element=driver.FindElement(By.id(BuyNowButton));
#By Class Name
List<WebElements> buttons=driver.FindElements(By.className("buttons"));
#By Tag Name
WebElement iframe=driver.FindElement(By.tagName("iframe"));
#By Name
WebElement name=driver.FindElement(By.name("firstName"));
#By LinkText
<a href="www.google.com/selenium.php">open-selenium</a>
WebElement link=driver.FindElement(By.linkText("open-selenium"));
#By Partial Link Text
<a href="www.google.com/selenium_setup.php">open selenium webdriver setup</a>
WebElement link=driver.FindElement(By.partialLinkText("setup"));
#By CSS [NEED MORE INFO ???]
-->CSS may vary from browser to browser hence not prefered.
-->browser native support for CSS will be used and if brwoser not supporting CSS then "Sizzle" will be used. eg IE 6,7 and FF 3.0 use Sizzle as search engine.
<div id= "food" ><span class= "dairy" >milk</span><span class= "dairy aged" >cheese</span>
WebElement cheese = driver.findElement(By.cssSelector( "#food span.dairy.aged" ));
#By Xpath
WebElement element = driver.findElements(By.xpath("//div[contains(text(),'Click Payment History')]"))
WebElement element = driver.findElements(By.xpath("//div[@class=\"articleHeader\"])).get(2);
#To get Hidden field
//input[@type="hidden"][@id="FinalPrice"]/@value
//input[@id="FinalPrice"]@value
#Relative XPath:
<table id="serviceInvocations" width="100%" style="display: block; height: 250px; overflow: auto;">
A: ".//*[@id='serviceInvocations']/tbody/tr[3]/td[1]"
#Xpath Functions:
//div[@id='summaryTip']//div[@class='xpop-content'][text()='You will see a summary of your transfer as you proceed. ']
driver.findElement(By.xpath(".//*[contains(text(),'Sign out')]")).click();
Few More - Xpath:
1. Verify button visibilty [class='select'] get appended if button is enable
String xpBrands = "//ul[@id='views']/li[@class='selected' and position()=1]/div/p/a";
2. Verify count of similar element
String xpTopScrollLinks = "//div[@id='topscrolllinks']/div/a";
int topScrollLinksCount = driver.findElements(By.xpath(xpTopScrollLinks)).size();
//div[@class='promote_favorite']/div[@id='fav_brand_775321 and @class='favorite faved']
Regards
Sheetal :)