Description:
Intention of this post is to track high level concepts, components needed to understand Selenium-2 or Web driver. This can be considered as hand out for quick reference. Examples or demo of using it in real code will be covered in subsequent posts.
What is Selenium-2 or Web Driver:
Development stacks:
How to setup:
High level overview:
Links:
Intention of this post is to track high level concepts, components needed to understand Selenium-2 or Web driver. This can be considered as hand out for quick reference. Examples or demo of using it in real code will be covered in subsequent posts.
What is Selenium-2 or Web Driver:
Development stacks:
- Java - 1.8
- Eclipse IDE - neon
- Windows - 2012 R2
How to setup:
- Download Selenium web driver. I used Selenium 3.8.1 for Java. This will download a zip file which has 'client-combined-3.8.1-sources.jar' and 'client-combined-3.8.1.jar' jars at root level. Copy these two jars along with all the jars from lib folder. Check Link-1 below.
- Download the browser driver intent to use for testing. In my case, I have downloaded web driver for IE, chrome and firefox as my application has to support these browser and phantomJS to run testing on headless browser to support deployment/testing automation. Check Link-2 below.
High level overview:
- WebDriver commands
- Fetching a page
- Different ways
- driver.get("http://www.google.com");
- driver.navigate().to("http://www.google.com");
- Activity on browser
- Closes the current browser associated with the driver. driver.close();
- Quit the driver and close all associated windows. driver.quit();
- Refresh the current page. driver.refresh();
- Locate UI element named as WebElement. 'Find Element' and Find Elements' method available in webdrvier or WebElement which takes locator or query object called 'By'
- Different 'By' strategies are
- By ID
- By Name
- By Class
- By tag name
- By name
- By link text
- By xpath
- Using javascript
- and more
- User interactions - check Link-5 for examples
- Textbox
- RadioButton
- CheckBox
- Drop down item selection
- Keyboard action
- Drag and drop
- Mouse action
- Multi selection
- Find all links
- Moving between windows and frames
- Examples
- driver.switchTo().window("windowName");
- driver.switchTo().frame("frameName");
- Popup dialogs
- Examples
- Alert alert = driver.switchTo().alert();
- Navigation: History and Location
- Examples
- driver.navigate().to("http://www.google.com");
- driver.navigate().forward();
- driver.navigate().back();
- Manage cookies. Add/delete/list all cookies
- Synchronization
- Old fashioned java way using 'Thread.sleep(xxx);' This will halt the execution for specified milliseconds.
- Implicit wait - the web driver will wait for specified time and will not try to find the element before the time is elapsed. After the time is crossed, web driver will try to find the element, execution will continue on success, throws exception on failure. This wait works at global level for all web element search.
- Example:
- WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); - Fluent wait - defines maximum amount of time to wait before a condition take place as well as the frequency to check the condition
- Example:
- Wait wait = new FluentWait(driver).withTimeout(60, SECONDS).pollingEvery(10, SECONDS).ignoring(NoSuchElementException.class);
WebElement dynamicelement = wait.until(new Function <webdriver,webElement>() {
public WebElement apply(WebDriver driver) { return driver.findElement(By.id("dynamicelement")); } }); - Explicit wait - set a certain condition to occur before move to next step. The condition will be periodically checked (by default every 500ms) until the specified timeout is reached which is defined during explicit wait creation. Common use is to perform an action based on element is visible. WebDriverWait extends FluentWait.
- Example:
- WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
Links:
- Selenium download page - http://www.seleniumhq.org/download/
- Download web driver - check 'Third Party Drivers, Bindings, and Plugins' section of Link-1
- http://www.seleniumhq.org/docs/01_introducing_selenium.jsp
- https://www.tutorialspoint.com/selenium/
- https://www.tutorialspoint.com/selenium/selenium_user_interactions.htm