What is webdriver?
WebElement myElem = findElement("prop");
If facebook failed to load within 60sec then script will fail.
WebDriver is a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.
2. What are the advantages of selenium2.0/webdriver?
- Need no server to start
- Easy to code
- Has sophisticated API to support wide verity of browsers.
- Supports to test dynamic UI web apps.
3. Difference between the selenium1.0 and selenium 2.0?
Selenium 1.0
|
Selenium 2.0/Webdriver
|
1. It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser.
2. Selenium server need to start to run tests
3. Has some loop-holes in supporting complex UI apps,Javascript security
4. No support for headless broswers
|
1. WebDriver makes direct calls to the browser using each browser’s native support for automation
2. Not needed unless tests are run on local machine.
3. Supports even drag and drop features and no security loop-holes
4. Supports htmlunit driver –headless browser runs fast
|
4. What are the Locators are there in selenium 2.0?
It supports locators based on Id,name,xpath,dom,css,class,tagname
5. How to handle the Ajax Applications in Web driver?
There are 2 types of waits webdriver supports to handle ajax applications to make webdrive sync to execution:
Implicit wait :
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
Explicit wait: WebDriverWait, FluentWait
WebElement strr = (new WebDriverWait(driver,30)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[starts-with(@id,'yui_3_4_0_1_137509')]/ul/li[2]/a")));
This link explains better about handling ajax in webdriver.
5. How to handle the multiple windows in web driver?
driver.switchTo().window(Window ID);
6. Difference between findelement() and findelements()?
findELement will find the first matching element.
findELements will all the matching elements. You'll probably need to loop through all the elements returned.
findELements will all the matching elements. You'll probably need to loop through all the elements returned.
7. How to handle the alerts in web driver?
driver.switchTo().alert().accept();
8. How to take the screen shots in seelnium2.0?
File src2 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src2,new File("d:\\sc2.jpeg"));
9. What is web driver architecture?
Below link gives better idea of webdriver architecture:
http://www.aosabook.org/en/selenium.html
10. What is the limitations of web driver?
- Can not automate desktop applications, supports only web applications
- No inbuilt commands to generate good reports
- Cannot support readily for new browsers
Technical challenges with selenium
As you know Selenium is a free ware open source testing tool.
There are many challenges with Selenium.
1.Selenium Supports only web based applications
2.It doesn’t support any non web based (Like Win 32, Java Applet, Java Swing, .Net Client Server etc) applications
3.When you compare selenium with QTP, Silk Test, Test Partner and RFT, there are many challenges in terms of maintainability of the test cases
4.Since Selenium is a freeware tool, there is no direct support if one is in trouble with the support of applications
5.There is no object repository concept in Selenium, so maintainability of the objects is very high
6.There are many challenges if one have to interact with Win 32 windows even when you are working with Web based applications
7.Bitmap comparison is not supported by Selenium
8.Any reporting related capabilities, you need to depend on third party tools
9.You need to learn any one of the native language like (.Net, Java, Perl, Python, PHP, Ruby) to work efficiently with the scripting side of selenium
1.Selenium Supports only web based applications
2.It doesn’t support any non web based (Like Win 32, Java Applet, Java Swing, .Net Client Server etc) applications
3.When you compare selenium with QTP, Silk Test, Test Partner and RFT, there are many challenges in terms of maintainability of the test cases
4.Since Selenium is a freeware tool, there is no direct support if one is in trouble with the support of applications
5.There is no object repository concept in Selenium, so maintainability of the objects is very high
6.There are many challenges if one have to interact with Win 32 windows even when you are working with Web based applications
7.Bitmap comparison is not supported by Selenium
8.Any reporting related capabilities, you need to depend on third party tools
9.You need to learn any one of the native language like (.Net, Java, Perl, Python, PHP, Ruby) to work efficiently with the scripting side of selenium
How to identify dynamic element in selenium
Dynamic element on their web pages where Ids of the elements gets generated dynamically. Each time id gets generated differently. So to handle this situation we use some JavaScript functions.
starts-with
if your dynamic element's ids have the format <button id="continue-12345" /> where 12345 is a dynamic number you could use the following
starts-with
if your dynamic element's ids have the format <button id="continue-12345" /> where 12345 is a dynamic number you could use the following
XPath: //button[starts-with(@id, 'continue-')]
contains
Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
To demonstrate, the element <input class="top suggest business"> can be located based on the ‘suggest’ class without having to couple it with the ‘top’ and ‘business’ classes using the following
Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
To demonstrate, the element <input class="top suggest business"> can be located based on the ‘suggest’ class without having to couple it with the ‘top’ and ‘business’ classes using the following
XPath: //input[contains(@class, 'suggest')].
Handling page load timeout
We can set the page loadtime in webdriver.Suppose if we want facebook to be loaded within 60 sec then add below statement in your code
sample code:
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.get("http://facebook.com/");
sample code:
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.get("http://facebook.com/");
WebElement myElem = findElement("prop");
try{
myElem.click()
}catch(Exception e){
sysout("Exception");
}
If facebook failed to load within 60sec then script will fail.
If you do not want script to fail in the above condition, put the code in try catch block and eat the exception. Make sure you know the timing of your page load or after page load wait for the elements to appear. It may make your test scripts flaky.
Selecting a date from Datepicker using Selenium WebDriver
Calendars look pretty and of course they are fancy too.So now a days most of the websites are using advanced jQuery Datepickers instead of displaying individual dropdowns for month,day,year. :P
Datepicker
If we look at the Datepicker, it is just a like a table with set of rows and columns.To select a date ,we just have to navigate to the cell where our desired date is present.
Here is a sample code on how to pick a 13th date from the next month.
view plainprint?
import java.util.List;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;;
public class DatePicker {
WebDriver driver;
@BeforeTest
public void start(){
System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
}
@Test
public void Test(){
driver.get("http://jqueryui.com/datepicker/");
driver.switchTo().frame(0);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//Click on textbox so that datepicker will come
driver.findElement(By.id("datepicker")).click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//Click on next so that we will be in next month
driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div/a[2]/span")).click();
/*DatePicker is a table.So navigate to each cell
* If a particular cell matches value 13 then select it
*/
WebElement dateWidget = driver.findElement(By.id("ui-datepicker-div"));
List<webelement> rows=dateWidget.findElements(By.tagName("tr"));
List<webelement> columns=dateWidget.findElements(By.tagName("td"));
for (WebElement cell: columns){
//Select 13th Date
if (cell.getText().equals("13")){
cell.findElement(By.linkText("13")).click();
break;
}
}
}
}</webelement></webelement>
Calendars look pretty and of course they are fancy too.So now a days most of the websites are using advanced jQuery Datepickers instead of displaying individual dropdowns for month,day,year. :P
Datepicker
If we look at the Datepicker, it is just a like a table with set of rows and columns.To select a date ,we just have to navigate to the cell where our desired date is present.
Here is a sample code on how to pick a 13th date from the next month.
view plainprint?
import java.util.List;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;;
public class DatePicker {
WebDriver driver;
@BeforeTest
public void start(){
System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
}
@Test
public void Test(){
driver.get("http://jqueryui.com/datepicker/");
driver.switchTo().frame(0);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//Click on textbox so that datepicker will come
driver.findElement(By.id("datepicker")).click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//Click on next so that we will be in next month
driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div/a[2]/span")).click();
/*DatePicker is a table.So navigate to each cell
* If a particular cell matches value 13 then select it
*/
WebElement dateWidget = driver.findElement(By.id("ui-datepicker-div"));
List<webelement> rows=dateWidget.findElements(By.tagName("tr"));
List<webelement> columns=dateWidget.findElements(By.tagName("td"));
for (WebElement cell: columns){
//Select 13th Date
if (cell.getText().equals("13")){
cell.findElement(By.linkText("13")).click();
break;
}
}
}
}</webelement></webelement>
ü Introduction
ü Locating an element using the findElementmethod
ü Locating elements using findElementsmethod
ü Locating links
ü Locating elements using CSS selectors
ü Locating elements using XPath
ü Locating elements using text
ü Locating elements using advanced CSS selectors
ü Using jQuery selectors
ü Locating table rows and cells
ü Locating child elements in a table
- WebDriver supports for below are Browsers
- ChromeDriver
- InternetExplorerDriver
- FirefoxDriver
- OperaDriver and
- HtmlUnitDriver
- There is also support for mobile testing –
- AndroidDriver
- OperaMobileDriver and
- IPhoneDriver.
Selenium Grid allows you to:
- scale by distributing tests on several machines ( parallel execution )
- Manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers / OS.
- Minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage virtual infrastructure for instance.
Step 1: Start the hub
- The Hub is the central point that will receive all the test request and distribute them the the right nodes.
- Open a command prompt and navigate to the directory where you copied the selenium-server-standalone file. Type the following command:
- java -jar selenium-server-standalone-2.14.0.jar -role hub
- The hub will automatically start-up using port 4444 by default. To change the default port, you can add the optional parameter -port when you run the command. You can view the status of the hub by opening a browser window and navigating to: http://localhost:4444/grid/console
Step 2: Start the nodes
- Regardless on whether you want to run a grid with new WebDriver functionality, or a grid with Selenium 1 RC functionality, or both at the same time, you use the same selenium-server-standalone jar file to start the nodes.
- java -jar selenium-server-standalone-2.14.0.jar -role node -hub http://localhost:4444/grid/register
- Note: The port defaults to 5555 if not specified whenever the "-role" option is provided and is not hub.
- For backwards compatibility "wd" and "rc" roles are still a valid subset of the "node" role. But those roles limit the types of remote connections to their corresponding API, while "node" allows both RC and WebDriver remote connections.
- For WebDriver nodes, you will need to use the RemoteWebDriver and the DesiredCapabilities object to define which browser, version and platform you wish to use. Create the target browser capabilities you want to run the tests against:
- DesiredCapabilities capability = DesiredCapabilities.firefox();
- Pass that into the RemoteWebDriver object:
- WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
Verifying ToolTip information by enabling javascript in browser
These days most of websites are using the ToolTip to provide information to end user.
ToolTip(also known as ScrrenTip) will be visible to the user whenever he/she mouseovers on specific object and it just displays information about the object(button,textbox,link,image etc..).
These tooltips works only when javascript is enabled.
So automating tooltip involves :
Verifying that tooltip presents when we mouse over on specific object
Verifying that the text that present in the tooltip is correct.
In this site if you mouse over on "Download" link we will see a tooltip.
Here is the program which will check the existence of tooltip and also the text present in the tooltip. In this program I have used HtmlDriver because it provides inbuilt methods for enabling and disabling of javascript.
Sample Code as Below:
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class HTMLdriver {
public static void main(String args[]){
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/");
WebElement element=driver.findElement(By.linkText("Download"));
Actions builder = new Actions(driver); // Configure the Action
Action mouseOver =builder.moveToElement(element).build(); // Get the action
mouseOver.perform(); // Execute the Action
if(driver.findElement(By.id("tooltip")).isDisplayed()){
System.out.println("Tooltip is coming up fine");
System.out.println("Here is the tool tip text : "+driver.findElement(By.id("tooltip")).getText());
}else{
System.out.println("There is no Tool tip text");
}
}
These days most of websites are using the ToolTip to provide information to end user.
ToolTip(also known as ScrrenTip) will be visible to the user whenever he/she mouseovers on specific object and it just displays information about the object(button,textbox,link,image etc..).
These tooltips works only when javascript is enabled.
So automating tooltip involves :
Verifying that tooltip presents when we mouse over on specific object
Verifying that the text that present in the tooltip is correct.
In this site if you mouse over on "Download" link we will see a tooltip.
Here is the program which will check the existence of tooltip and also the text present in the tooltip. In this program I have used HtmlDriver because it provides inbuilt methods for enabling and disabling of javascript.
Sample Code as Below:
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class HTMLdriver {
public static void main(String args[]){
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/");
WebElement element=driver.findElement(By.linkText("Download"));
Actions builder = new Actions(driver); // Configure the Action
Action mouseOver =builder.moveToElement(element).build(); // Get the action
mouseOver.perform(); // Execute the Action
if(driver.findElement(By.id("tooltip")).isDisplayed()){
System.out.println("Tooltip is coming up fine");
System.out.println("Here is the tool tip text : "+driver.findElement(By.id("tooltip")).getText());
}else{
System.out.println("There is no Tool tip text");
}
}