Getting up and running with
Selenium for Automated Web
Testing

Emma Armstrong
Website: www.taooftesting.co.uk

@EmmaATester
Continuous Delivery
Automate and improve software delivery process

Rapidly push out enhancements
to customers
Quick and reliable release cycles
Automated testing

Image from http://en.wikipedia.org/wiki/Postman_Pat
Where to start?
Selenium
• Works with most browsers and is multi platform (c#,
Java, Ruby)
• WebDriver /IDE
• Nuget packages
• Selenium.Support
• Selenium.WebDriver

• Grid capabilities
• Hub and clients
• Simple jar file install commands
Selenium WebDriver
• Add Selenium to your project
• Add Selenium to your class
• Using OpenQA.Selenium;
• Using OpenQA.Selenium.Firefox;

• Create an instance of the WebDriver
• IWebDriver driver = new FirefoxDriver();

• Interact with that driver
• Go to a web page
• Driver. Navigate().GoToUrl(“http://localhost:82”);

• Find an element on the page
• Driver. FindElement(By.Id("Username"))
NUnit
• Add NUnit to your project
• Add NUnit to your class
• Using Nunit.framework;

• Attributes
•
•
•
•

[TestFixture] – indicates the class contains test code
[Test] – indicates that it is a test method
[TestCase]
[TestFixtureSetup] – performed once prior to the tests in that test
fixture being run to initialise
• [TestFixtureTearDown] - performed once after the tests in that test
have run to clean up
Test classes must be public and have a default constructor

• Assert class
• Defines a set of methods you can use to check the post condition
Eating your own dog food
Demo
PageObject Model
• Create a class for each page in your application
• Extract common elements and web elements to a Base
class that the pages can inherit
• Create your base class
• Add selenium support
• Using OpenQA.Selenium.Support.PageObjects;

• Define the elements on the page
• [FindsBy(How = How.Id, Using = "footer_version")] private IWebElement
version;

• Initialise the elements on the page
• PageFactory.InitElements(driver, this);
Nunit test
Example c# nunit test
using System;
using NUnit.Framework;
using RedGate.Deploy.WebAppTests.Pages;
namespace RedGate.Deploy.SmokeTests
{
[TestFixture]
public class VersionTest : SmokeTestBase
{
[Test]
public void LoginPageShowsCurrentVersion()
{
Version expectedVersion = GetType().Assembly.GetName().Version;
LoginPage loginPage = LoginPage.Load(Driver, SmokeTestUrlBase);
Assert.AreEqual("v" + expectedVersion, loginPage.VersionNumber.Trim());
}
Demo
Example Java test
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Import org.openqa.selenium.firefox.firefoxdriver;
Public class javaExample {
public static void main(String[] args) throws Exception {
Webdriver driver = new firefoxdriver();
driver.get("http://localhost:8080");
webelement loginname = driver.findelement(By.id("Username"));
loginname.sendkeys("loginname");
Webelement password = driver.findelement(By.id("Password"));
password.sendkeys("loginname");
Webelement loginbutton = driver.findelement(By.id("loginbutton"));
loginbutton.submit();
driver.quit();
}
}
Selenium Grid Configuration
Install
• java -jar selenium-server-standalone-2.14.0.jar -role hub
• java -jar selenium-server-standalone-2.14.0.jar -role node -hub
http://localhost:4444/grid/register
Usage
• browser browserName=firefox, version=3.6,platform=LINUX
• DesiredCapabilities capability = DesiredCapabilities.firefox();
• webDriver driver = new RemoteWebDriver(new
URL("http://localhost:4444/wd/hub"), capability);
Summary
• Improve confidence in your deployment
• Adding tests is easier than you might think
• Automate gradually
</talk>
Code:
https://github.com/EmmaArmstrong/SeleniumTesting

Emma Armstrong
@EmmaATester

Getting up and running with selenium for automated Code palousa

  • 1.
    Getting up andrunning with Selenium for Automated Web Testing Emma Armstrong Website: www.taooftesting.co.uk @EmmaATester
  • 2.
    Continuous Delivery Automate andimprove software delivery process Rapidly push out enhancements to customers Quick and reliable release cycles Automated testing Image from http://en.wikipedia.org/wiki/Postman_Pat
  • 3.
  • 4.
    Selenium • Works withmost browsers and is multi platform (c#, Java, Ruby) • WebDriver /IDE • Nuget packages • Selenium.Support • Selenium.WebDriver • Grid capabilities • Hub and clients • Simple jar file install commands
  • 5.
    Selenium WebDriver • AddSelenium to your project • Add Selenium to your class • Using OpenQA.Selenium; • Using OpenQA.Selenium.Firefox; • Create an instance of the WebDriver • IWebDriver driver = new FirefoxDriver(); • Interact with that driver • Go to a web page • Driver. Navigate().GoToUrl(“http://localhost:82”); • Find an element on the page • Driver. FindElement(By.Id("Username"))
  • 6.
    NUnit • Add NUnitto your project • Add NUnit to your class • Using Nunit.framework; • Attributes • • • • [TestFixture] – indicates the class contains test code [Test] – indicates that it is a test method [TestCase] [TestFixtureSetup] – performed once prior to the tests in that test fixture being run to initialise • [TestFixtureTearDown] - performed once after the tests in that test have run to clean up Test classes must be public and have a default constructor • Assert class • Defines a set of methods you can use to check the post condition
  • 7.
  • 8.
  • 9.
    PageObject Model • Createa class for each page in your application • Extract common elements and web elements to a Base class that the pages can inherit • Create your base class • Add selenium support • Using OpenQA.Selenium.Support.PageObjects; • Define the elements on the page • [FindsBy(How = How.Id, Using = "footer_version")] private IWebElement version; • Initialise the elements on the page • PageFactory.InitElements(driver, this);
  • 10.
    Nunit test Example c#nunit test using System; using NUnit.Framework; using RedGate.Deploy.WebAppTests.Pages; namespace RedGate.Deploy.SmokeTests { [TestFixture] public class VersionTest : SmokeTestBase { [Test] public void LoginPageShowsCurrentVersion() { Version expectedVersion = GetType().Assembly.GetName().Version; LoginPage loginPage = LoginPage.Load(Driver, SmokeTestUrlBase); Assert.AreEqual("v" + expectedVersion, loginPage.VersionNumber.Trim()); }
  • 11.
  • 12.
    Example Java test importorg.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; Import org.openqa.selenium.firefox.firefoxdriver; Public class javaExample { public static void main(String[] args) throws Exception { Webdriver driver = new firefoxdriver(); driver.get("http://localhost:8080"); webelement loginname = driver.findelement(By.id("Username")); loginname.sendkeys("loginname"); Webelement password = driver.findelement(By.id("Password")); password.sendkeys("loginname"); Webelement loginbutton = driver.findelement(By.id("loginbutton")); loginbutton.submit(); driver.quit(); } }
  • 13.
    Selenium Grid Configuration Install •java -jar selenium-server-standalone-2.14.0.jar -role hub • java -jar selenium-server-standalone-2.14.0.jar -role node -hub http://localhost:4444/grid/register Usage • browser browserName=firefox, version=3.6,platform=LINUX • DesiredCapabilities capability = DesiredCapabilities.firefox(); • webDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
  • 14.
    Summary • Improve confidencein your deployment • Adding tests is easier than you might think • Automate gradually
  • 15.

Editor's Notes

  • #2 AboutUmbracoEmmaArmstrngTest EngineerRed Gate
  • #5 java -jar selenium-server-standalone-2.14.0.jar -role hub java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://localhost:4444/grid/registerbrowser  browserName=firefox, version=3.6,platform=LINUXDesiredCapabilities capability = DesiredCapabilities.firefox();ebDriver driver = new RemoteWebDriver(new URL(&quot;http://localhost:4444/wd/hub&quot;), capability);
  • #6 java -jar selenium-server-standalone-2.14.0.jar -role hub java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://localhost:4444/grid/registerbrowser  browserName=firefox, version=3.6,platform=LINUXDesiredCapabilities capability = DesiredCapabilities.firefox();ebDriver driver = new RemoteWebDriver(new URL(&quot;http://localhost:4444/wd/hub&quot;), capability);