Skip to content

Instantly share code, notes, and snippets.

@r-sal
Last active August 9, 2023 20:45
Show Gist options
  • Save r-sal/4273784 to your computer and use it in GitHub Desktop.
Save r-sal/4273784 to your computer and use it in GitHub Desktop.
Selenium & WebDriver Notes

Sort

Data Driven
http://automationtestingsimplified.wordpress.com/2011/05/31/hybrid-testing-data-keyword-driven-using-selenium/ How to do Data Driven Testing using Selenium 2 (WebDriver)?
DataProvider - Data Driven Testing with Selenium and TestNG

Excel
http://sourceforge.net/projects/jexcelapi/files/
Java Excel API
http://testng.org/doc/documentation-main.html#parameters-dataproviders

Web Driver - Tips and Tricks

Grid
Starting with Selenium GRID

New Site
http://paper.li/TheWebTester/1330032206

Various
http://selenium.polteq.com/en/capturing-javascript-errors-with-selenium-webdriver/
http://selenium.polteq.com/en/perform-a-sequence-of-actions-with-selenium-webdriver/
http://selenium.polteq.com/en/highlight-elements-with-selenium-webdriver/
http://selenium.polteq.com/en/create-a-screenshot-of-webelements-with-webdriver/
http://jbehave.org/

Page Objects
http://blog.activelylazy.co.uk/2011/07/09/page-objects-in-selenium-2-0/ http://sqa.stackexchange.com/questions/571/page-objects-design-issues http://selenium-tutorial.blogspot.com/2012/06/webdriver-page-objects-pattern.html

Forms
http://selenium.polteq.com/en/controlling-a-selectbox-with-selenium-webdriver/
http://selenium-tutorial.blogspot.com/2012/10/select-check-checkbox-webdriver.html
http://selenium-tutorial.blogspot.com/2012/04/uploading-file.html

Performance Testing
http://selenium.polteq.com/en/using-dynatrace-to-measure-the-performance/

Mobile Testing
http://selenium.polteq.com/en/running-against-a-real-iphone-device-2/
http://selenium.polteq.com/en/running-tests-on-a-xcode-iphone-simulator-3/
http://selenium.polteq.com/en/running-tests-on-a-real-android-device-2/
http://selenium.polteq.com/en/running-tests-on-an-android-simulator/

Test Cases - Design Patterns

Page Objects

How should a common part of a page be treated within the page object pattern?

PageObject need not represent an entire page. It may represent a section that appears many times within a site or page, such as site navigation.

The essential principle is that there is only one place in your test suite with knowledge of the structure of the HTML of a particular (part of a) page.

Selenium - PageObjects http://seleniumhq.org/docs/06_test_design_considerations.jsp#page-object-design-pattern

There is a lot of flexibility in how the page objects may be designed, but there are a few basic rules for getting the desired maintainability of your test code. Page objects themselves should never be make verifications or assertions. This is part of your test and should always be within the test’s code, never in an page object. The page object will contain the representation of the page, and the services the page provides via methods but no code related to what is being tested should be within the page object.

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. This verification should be done while instantiating the page object. In the examples above, both the SignInPage and HomePage constructors check that the expected page is available and ready for requests from the test.

A page object does not necessarily need to represent an entire page. The Page Object design pattern could be used to represent components on a page. If a page in the AUT has multiple components, it may improved maintainability if there was a separate page object for each component.

There are other design patterns that also may be used in testing. Some use a Page Factory for instantiating their page objects. Discussing all of these is beyond the scope of this user guide. Here, we merely want to introduce the concepts to make the reader aware of some of the things that can be done. As was mentioned earlier, many have blogged on this topic and we encourage the reader to search for blogs on these topics.

Summary

  • The public methods represent the services that the page offers
  • Try not to expose the internals of the page
  • Generally don't make assertions
  • Methods return other PageObjects
  • Need not represent an entire page
  • Different results for the same action are modelled as different methods

Page Factory

http://code.google.com/p/selenium/wiki/PageFactory

Selenium


Web Driver

Drivers

HtmlUnit

WebDriver driver = new HtmlUnitDriver(true);  // True Enables Javascript  

Internet Explorer Driver

Firefox

Modifying the Firefox Profile

WebDriver driver = new FirefoxDriver();
ProfileIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver");
profile.setPreferences("foo.bar", 23);
WebDriver driver = new FirefoxDriver(profile);

Running with Firebug

File file = new File("firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); // Avoid startup screen
WebDriver driver = new FirefoxDriver(firefoxProfile);

Android

iPhone Driver

Blackberry

TBD

Locating page elements using WebDriver

driver.findElement(locatorStrat);

Table for two

CSSBy.css("input[class~='required']")
XpathBy.xpath("//input[contains(@class='required') and type='text']")
By.linkText("Sign In")
By.partialLinkText("Sign")
By.name("login")
By.id("#someId")
---

##How to Capture the network traffic You can capture network traffic using a proxy, such as the BrowserMob Proxy (http://proxy.browsermob.com)

To configure the use of the proxy with a webdriver instance, set the CapabilityName.PROXY value to a org.openqa.selenium.Proxy instance:

Proxy proxy = new Proxy();
// The URL here is the URL that the browsermob proxy is using
proxy.setHttpProxy("localhost:9100");

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);

WebDriver driver = new FirefoxDriver(capabilities);

Once the test is finished, you can extract the data from the browsermob proxy using the mechanisms it provides. Notice that you can also use any proxy you want: it need not be the browsermob one!

Articles

How to WebDriverWait


### FluentWait http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html
Wait wait = new FluentWait(driver)  
  .withTimeout(30, SECONDS)  
  .pollingEvery(2, SECONDS);  
  
wait.until(ExpectedConditions.titleIs("Login")); 

Wrapping Selenium Calls

‘Safe Operations’ for Element Presence

/**
 * Clicks and Waits for page to load.
 *
 * param elementLocator
 * param waitPeriod
 */
public void clickAndWait(String elementLocator, String waitPeriod) {
        selenium.click(elementLocator);
        selenium.waitForPageToLoad(waitPeriod);
}
/**
 * Selenium-WebDriver -- Clicks on element only if it is available on page.
 *
 * param elementLocator
 */
public void safeClick(String elementLocator) {
        WebElement webElement = getDriver().findElement(By.XXXX(elementLocator));
        if(webElement != null) {
                selenium.click(elementLocator);
        } else {
                // Using the TestNG API for logging
                Reporter.log("Element: " +elementLocator+ ", is not available on page - "
                                + getDriver().getUrl());
        }
}
@raja45
Copy link

raja45 commented Oct 3, 2016

!DOCTYPE html>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="viewport" content=""> 
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<title>Selenium online Training|Selenium classroom Training HYd|USA|UK|Canada</title>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment