Selenium WebDriver

What is WebDriver?

 WebDriver is a tool for writing automated tests of websites. It aims to mimic the behaviour of a real user, and as such interacts with the HTML of the application.


Driving a browser natively as a user would do


object + actions = "real life" scenario

OBJECTS(WebElements)


Can be identified by:

  • By ID

  • By CSS

  • By XPATH

  • By Partial Link Text

  • By Link Text

  • By Name

  • By Class Name



How to identify your web elements?


ID

driver.find_element_by_id("id_password")


CSS

driver.find_element_by_css_selector("#move-target-{} > a.move-target.last-child")


XPATH

//div[contains(@class, 'article-heading')]
//input[starts-with(@id, 'text-')]

ACTIONS



What are the basic actions a user can do in a page?

  • click
  • selections
  • type (submit forms)
  • check options


API


Why writing your own actions on top of existent ones?

  • you determine when test will fail
  • logging your actions - know what happens
  • more descriptive code


Ex

 def clickWebElement (self, by_what, wel_path, wait_time):
tmpWEL = self.detectWEL(by_what, wel_path, wait_time)
if tmpWEL is not False:
tmpWEL.click()
tmpWEL = None
else:
print "clickWEL method error: the element identified by %s was not found" %wel_path
 def typeText (self, by_what, wel_path, wait_time, text_to_type):
tmpWEL = self.detectWEL(by_what, wel_path, wait_time)
if tmpWEL is not False:
tmpWEL.click()
tmpWEL.clear()
tmpWEL.send_keys(text_to_type)
tmpWEL = None
else:
print "typeText method error: the element identified by %s was not found" %wel_path

Assertions


To do assertion right for this simple case, we have to remember the important principle of writing good assertions - The goal when testing an implementation is to achieve consistent reproducible results that accurately indicate the conformance of the implementation to the specification. In another word: the test assertion will fail if and only if the tested function is broken.

 assertEquals( "http://www.thoughtworks.com/technology", browser.getLocation() );
assertEquals( "Technology - www.thoughtworks.com", browser.getTitle());
assertEquals(browser.getText("/html/body/div[@id='container']/div[3]/p[1]/span"), 'Technology');







How to organize your test project?


PageObjects   The Page Object pattern represents the screens of your web app as a series of objects


Procedural   Specifying the steps the program must take to reach the desired state






Why Selenium WebDriver?


Which browsers does WebDriver support?

The existing drivers are the ChromeDriver, InternetExplorerDriver, FirefoxDriver, OperaDriver and HtmlUnitDriver.

There is also support for mobile testing via the AndroidDriver, OperaMobileDriver and IPhoneDriver.


+ UserAgents  RWEB

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference(
"general.useragent.override",
"Mozilla/5.0 (Linux; U; en-US) AppleWebKit/528.5+ (KHTML, like Gecko, Safari/528.5+) Version/4.0 Kindle/3.0 (screen 600×800; rotate)");
driver = new FirefoxDriver(profile);

What about support for languages other than Java?

Python, Ruby, C# and Java are all supported directly by the development team. There are also webdriver implementations for PHP and Perl. Support for a pure JS API is also planned.







Selenium WebDriver

By bpirte

Selenium WebDriver

  • 1,716