Selenium WebDriver Hands-on solution

What is Selenium?

Selenium is a free (open-source) automated testing framework used to validate web applications across different browsers and platforms. You can use multiple programming languages like Java, C#, Python etc to create Selenium Test Scripts. Testing done using the Selenium testing tool is usually referred to as Selenium Testing.

Selenium Software is not just a single tool but a suite of software, each piece catering to different Selenium QA testing needs of an organization. Here is the list of tools

  • Selenium Integrated Development Environment (IDE)
  • Selenium Remote Control (RC)
  • WebDriver
  • Selenium Grid
Introduction to Selenium
Introduction to Selenium

At the moment, Selenium RC and WebDriver are merged into a single framework to form Selenium 2. Selenium 1, by the way, refers to Selenium RC. 

click here to more questions and answers

Click here to read more questions and answers

1.TestNG Annotation Hands-On

File Name – TestNG_Annotations.java

package Testng;

import org.testng.annotations.Test;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.AfterSuite;

public class TestNG_Annotations {

  // Write your code here

  @AfterSuite

  public void afterSuite(){

    System.out.println(“After Suite has been executed as no.9”);

  }

  @BeforeClass

  public void beforeClass(){

    System.out.println(“Before Class has been executed as no.3”);

  }

  @BeforeTest

  public void beforeTest(){

    System.out.println(“Before Test has been executed as no.2”);

  }

  @BeforeMethod

  public void beforeMethod(){

    System.out.println(“Before Method has been executed as no.4”);

  }

  @Test

  public void GetpageTitle(){

    System.out.println(“Test has been executed as no.5”);

  }

  @AfterMethod

  public void afterMethod(){

    System.out.println(“After Method has been executed as no.6”);

  }

  @AfterTest

  public void afterTest(){

    System.out.println(“After Test has been executed as no.8”);

  }

  @BeforeSuite

  public void beforeSuite(){

    System.out.println(“Before Suite has been executed as no.1”);

  }

  @AfterClass

  public void afterClass(){

    System.out.println(“After Class has been executed as no.7”);

  }

}

2.Hands-OnWelcome to Launch Browser with TestNG

File Name – LaunchBrowser_TestNG

package launchBrowserTestNG;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.phantomjs.PhantomJSDriver;

import org.openqa.selenium.phantomjs.PhantomJSDriverService;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.testng.annotations.Test;

public class LaunchBrowser_TestNG {

@Test

  public void LaunchBrowser() throws InterruptedException{

DesiredCapabilities caps = new DesiredCapabilities();

caps.setJavascriptEnabled(true); 

caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, “/projects/challenge/phantomjs-2.1.1-linux-x86_64/bin/phantomjs”);

WebDriver driver = new PhantomJSDriver(caps);

System.out.println(“PhantomJS Headless Driver launched”);

// Write your script here

                driver.get(“https:/google.com”);

                Thread.sleep(5000);

                System.out.println(“Launch Browser is successful”);

                System.out.println(“Page Title : ” + driver.getTitle());

//Searching for “Fresco Play” in Google search

driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(“Fresco Play”);

driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(Keys.ENTER);

Thread.sleep(5000);

System.out.println(“Page Title : ” + driver.getTitle());

}

}

File Name – pom.xml

<project xmlns=”http://maven.apache.org/POM/4.0.0″

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

<modelVersion>4.0.0</modelVersion>

<groupId>com</groupId>

<artifactId>launchBrowserTestNG</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>launchBrowserTestNG</name>

<url>http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<suiteXmlFile>launchBrowserTestNG/testng.xml</suiteXmlFile>

</properties>

<dependencies>

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>3.11.0</version>

</dependency>

<!– Add you Testng dependency here…!! –>

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.14.3</version>

                         <scope>test</scope>

</dependency>

<dependency>

<groupId>com.github.detro.ghostdriver</groupId>

<artifactId>phantomjsdriver</artifactId>

<version>1.0.1</version>

</dependency>

</dependencies>

<build>

   <plugins>

         <plugin>

             <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-compiler-plugin</artifactId>

             <version>3.0</version>

             <configuration>

                 <source>1.8</source>

                 <target>1.8</target>

             </configuration>

       </plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.22.0</version>

<configuration>

<suiteXmlFiles>

<suiteXmlFile>testng.xml</suiteXmlFile>

</suiteXmlFiles>

</configuration>

</plugin>

</plugins>

</build>

</project>

File Name – testng.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=”Suite”>

  <test thread-count=”5″ name=”Test”>

    <classes>

      <class name=”launchBrowserTestNG.LaunchBrowser_TestNG”/>

    </classes>

  </test> <!– Test –>

</suite> <!– Suite –>

3.Cross Browser – Hands-On

Welcome to Cross Browser Testing Selenium

File Name – googleLaunch.java

package crossBrowsers;

import org.testng.annotations.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.phantomjs.PhantomJSDriver;

import org.openqa.selenium.phantomjs.PhantomJSDriverService;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Parameters;

import io.github.bonigarcia.wdm.ChromeDriverManager;

public class googleLaunch {

    public WebDriver driver;

    @BeforeTest

    @Parameters(“browser”)

    public void SelectBrowser(String browser)

    {

        // Write your script here  

        if(browser.equalsIgnoreCase(“ChromeHeadless”))

        {

          ChromeDriverManager.chromedriver().setup();

          ChromeOptions chromeOptions = new ChromeOptions();

          chromeOptions.addArguments(“–headless”);

          System.out.println(“Chrome Headless Driver launched”);

        }

        else if(browser.equalsIgnoreCase(“PhantomJS”))

        {

          DesiredCapabilities caps = new DesiredCapabilities();

          caps.setJavascriptEnabled(true);

          caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, “/projects/challenge/phantomjs-2.1.1-linux-x86_64/bin/phantomjs”);

          driver = new PhantomJSDriver(caps);

          System.out.println(“PhantomJS Headless Driver launched”);

        }

        driver.manage().window().maximize();

        driver.get(“https://www.google.com”);

    }

    @Test

    public void GoogleSreach() throws InterruptedException{

        // Searching for “Fresco Play” in Google search.

        // Find the elementlocators for the below actions.

        // 1) Find locator for Google Search Box and sendkeys as Fresco Play as below.

        driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(“Fresco Play”);

        // 2) Find locator for Google Search Box and sendkeys as Enter  as below.

        driver.findElement(By.xpath(“//input[@name=’q’]”)).sendKeys(Keys.ENTER);

        Thread.sleep(6000);

        System.out.println(“Page Title : ” + driver.getTitle());

    }

    @AfterTest

    public void BroswerQuit(){

        driver.quit();

    }    

}

4.Hands-On: POM Normal Approach

Welcome to POM Normal Approach

File Name – LoginPage.java

package Pages;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

public class LoginPage {

WebDriver driver;

// write your script here.

By LoginTab=By.xpath(“//*[@id=’pt-login’]/a”);

By username=By.xpath(“//*[@id=’wpName1′]”);

By password=By.xpath(“//*[@id=’wpPassword1′]”);

By logIn=By.xpath(“//*[@id=’wpLoginAttempt’]”);

public LoginPage(WebDriver driver){

this.driver=driver;

}

public void clickLoginTab(){

driver.findElement(LoginTab).click();

}

public void typeUserName(){

driver.findElement(username).sendKeys(“”);

}

public void typePassword(){

driver.findElement(password).sendKeys(“”);

}

public void clickLoginButton(){

driver.findElement(logIn).click();

}

}

5.Hands-On: Page Factory Method

Welcome to Page Factory Method

File Name – verfiyLogin_PageFactory.java

package TestCases;

import org.testng.annotations.Test;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.support.PageFactory;

import Pages.PageFactoryLogin;

import utility.BrowserFactory;

public class verfiyLogin_PageFactory {

WebDriver driver;

@Test

public void checkValidUser(){

WebDriver driver = BrowserFactory.startBrowser(“PhantomJS”, “https://en.wikipedia.org/wiki/Main_Page”);

PageFactoryLogin login_page = PageFactory.initElements(driver, PageFactoryLogin.class);

// Write your username and password in below line

login_page.Login(“Abarna6898″,”abcdefg”);

System.out.println(“Page Title : ” + driver.getTitle());

driver.quit();

System.out.println(“Browser Quit is successful”);

}

}

File Name – BrowserFactory.java

package utility;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import org.openqa.selenium.phantomjs.PhantomJSDriver;

import org.openqa.selenium.phantomjs.PhantomJSDriverService;

import org.openqa.selenium.remote.DesiredCapabilities;

public class BrowserFactory {

static WebDriver driver;

public static WebDriver startBrowser(String browserName, String url ){

if (browserName.equalsIgnoreCase(“chrome”)){

System.setProperty(“webdriver.chrome.driver”, “/root/selenium/chromedriver”);

driver=new ChromeDriver();

}

else if (browserName.equalsIgnoreCase(“HtmlUnitDriver”)){

 driver = new HtmlUnitDriver(true);

}

else if (browserName.equalsIgnoreCase(“PhantomJS”))

{

 DesiredCapabilities caps = new DesiredCapabilities();

 caps.setJavascriptEnabled(true); 

             String [] phantomJsArgs = {“–ignore-ssl-errors=yes”};

             caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs); 

 caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, “/projects/challenge/phantomjs-2.1.1-linux-x86_64/bin/phantomjs”);

 driver = new PhantomJSDriver(caps);

System.out.println(“PhantomJS Headless Driver launched”);

}

// Write your script here

            driver.manage().window().maximize();

            driver.get(url);

return driver;

}

}

File Name – PageFactoryLogin.java

package Pages;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.How;

public class PageFactoryLogin {

WebDriver driver;

public PageFactoryLogin(WebDriver localdriver){

this.driver=localdriver;

}

// Write your script here

  @FindBy(xpath = “//*[@id=’pt-login’]/a”)WebElement loginTab;

  @FindBy(id = “wpName1”)WebElement username;

  @FindBy(how = How.ID,using = “wpPassword1”)WebElement password;

  @FindBy(how = How.XPATH,using = “//*[@id=’wpLoginAttempt’]”)WebElement logIn;

public void Login(String uid, String pass){

loginTab.click();

username.sendKeys(uid);

password.sendKeys(pass);

logIn.click();

}

}

Selenium Webdriver Hands-On

SAP S/4HANA Master Data Concepts Questions & Answers
SAP S/4HANA Functional Capabilities 1909 Questions & Answer
SAP S/4HANA Technical Capabilities 1909 Questions & Answers
SAP Implementation S/4HANA Project Management Questions & Answers
SAP S/4 HANA Enterprise Management – Sales Questions Answers
SAP S/4HANA Key Concepts Overview Questions & Answers
SAP S/4HANA Enterprise Structure Questions & Answer
SAP S/4HANA Reporting and Analytics Questions & Answers
SAP S/4HANA Navigation Interview Questions and Answers
SAP S/4HANA Awareness 1909 Questions and Answers

About Author


After years of Technical Work, I feel like an expert when it comes to Develop wordpress website. Check out How to Create a Wordpress Website in 5 Mins, and Earn Money Online Follow me on Facebook for all the latest updates.

Leave a Comment