WebDriver is an apparatus for mechanizing testing web applications. It is prevalently known as Selenium 2.0. WebDriver utilizes an alternate hidden system, while Selenium RC utilizes JavaScript Selenium-Core implanted inside the program which has got a few constraints. WebDriver interfaces straightforwardly with the program with no delegate, dissimilar to Selenium RC that relies upon a server. It is utilized in the accompanying setting −
- Multi-program testing including improved usefulness for programs which isn't very much upheld by Selenium RC (Selenium 1.0).
- Dealing with various casings, numerous program windows, popups, and alarms.
- Complex page route.
- Propelled client route, for example, intuitive.
- AJAX-based UI components.
Architecture
WebDriver is best clarified with a basic design outline as demonstrated as follows.
Selenium RC Vs WebDriver
Selenium RC | Selenium WebDriver |
---|---|
The architecture of Selenium RC is complicated, as the server needs to be up and running before starting a test. | WebDriver's architecture is simpler than Selenium RC, as it controls the browser from the OS level. |
Selenium server acts as a middleman between the browser and Selenese commands. | WebDriver interacts directly with the browser and uses the browser's engine to control it. |
Selenium RC script execution is slower, since it uses a Javascript to interact with RC. | WebDriver is faster, as it interacts directly with the browser. |
Selenium RC cannot support headless execution as it needs a real browser to work with. | WebDriver can support the headless execution. |
It's a simple and small API. | Complex and a bit large API as compared to RC. |
Less object-oriented API. | Purely object oriented API. |
Cannot test mobile Applications. | Can test iPhone/Android applications. |
Scripting using WebDriver
Let us see how to function with WebDriver. For show, we would utilize https://www.calculator.net/. We will play out a "Percent Calculator" which is situated under "Math Calculator". We have just downloaded the required WebDriver JAR's. Allude the part "Natural Setup" for subtleties.
Stage 1 − Launch "Shroud" from the Extracted Eclipse organizer.
Stage 2 − Select the Workspace by tapping the 'Peruse' button.
Stage 3 − Now make 'Another Project' from 'Document' menu.
Stage 4 − Enter the Project Name and Click 'Next'.
Stage 5 − Go to Libraries Tab and select all the JAR's that we have downloaded. Add reference to all the JAR's of Selenium WebDriver Library envelope and furthermore selenium-java-2.42.2.jar and selenium-java-2.42.2-srcs.jar.
Stage 6 − The Package is made as demonstrated as follows.
Stage 7 − Now right-click on the bundle and select 'New' >> 'Class' to make a 'class'.
Stage 8 − Now name the class and make it the primary capacity.
Stage 9 − The class plot is appeared as beneath.
Stage 10 − Now the time has come to code. The accompanying content is more obvious, as it has remarks implanted in it to clarify the means plainly. If it's not too much trouble investigate the part "Locators" to see how to catch object properties.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class webdriverdemo {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
//Puts an Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch website
driver.navigate().to("http://www.calculator.net/");
//Maximize the browser
driver.manage().window().maximize();
// Click on Math Calculators
driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click();
// Click on Percent Calculators
driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a")).click();
// Enter value 10 in the first number of the percent Calculator
driver.findElement(By.id("cpar1")).sendKeys("10");
// Enter value 50 in the second number of the percent Calculator
driver.findElement(By.id("cpar2")).sendKeys("50");
// Click Calculate Button
driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr[2]/td/input[2]")).click();
// Get the Result Text based on its xpath
String result =
driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/font/b")).getText();
// Print a Log In message to the screen
System.out.println(" The Result is " + result);
//Close the Browser.
driver.close();
}
}
Stage 11 − The yield of the above content would be imprinted in Console.
Most Used Commands
The accompanying table records the absolute most regularly utilized orders in WebDriver alongside their punctuation.
Sr.No. | Command & Description |
---|---|
1 |
driver.get("URL") To navigate to an application. |
2 |
element.sendKeys("inputtext") Enter some text into an input box. |
3 |
element.clear() Clear the contents from the input box. |
4 |
select.deselectAll() Deselect all OPTIONs from the first SELECT on the page. |
5 |
select.selectByVisibleText("some text") Select the OPTION with the input specified by the user. |
6 |
driver.switchTo().window("windowName") Move the focus from one window to another. |
7 |
driver.switchTo().frame("frameName") Swing from frame to frame. |
8 |
driver.switchTo().alert() Helps in handling alerts. |
9 |
driver.navigate().to("URL") Navigate to the URL. |
10 |
driver.navigate().forward() To navigate forward. |
11 |
driver.navigate().back() To navigate back. |
12 |
driver.close() Closes the current browser associated with the driver. |
13 |
driver.quit() Quits the driver and closes all the associated window of that driver. |
14 |
driver.refresh() Refreshes the current page. |