Saturday, January 27, 2018

Selenium + Cucumber Integration


Cucumber is nice way of achieving automation. Below is a sample automation steps on logging into Facebook app & logging out of the application itself.

Note: Below sample includes :
            Selenium and Cucumber integration with passing parameters
            Data driven testing sample
            All reports generation for Cucumber
       

You have to have following Jar list :

cucumber-reporting-0.1.14-sources.jar
junit-4.12-sources.jar
cobertura-2.1.1.jar
cucumber-jvm-deps-1.0.3.jar
cucumber-core-1.2.2.jar
cucumber-junit-1.2.2.jar
cucumber-java-1.2.2.jar
cucumber-html-0.2.2-sources.jar

Data driven testing sample
===================

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
// glue - here you give the full qualified package name
@CucumberOptions(features="features",
glue="feature.description",
plugin={"html:target/cucumber-html-report",
"json:target/cucumber.json",
"pretty:target/cucumber-pretty.txt",
"usage:target/cucumber-usage.json",
"junit:target/cucumber-results.xml"})
public class TestRunner {

}


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class SampleTest {

    private WebDriver driver;

@Given("^Open \"([^\"]*)\" and the \"([^\"]*)\" start \"([^\"]*)\" application$")
public void Open_chrome_and_start_application(String driverName ,String driverLocation, String applicationName) throws Throwable {

    System.setProperty(driverName, driverLocation);

            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--disable-notifications");

    driver = new ChromeDriver(chromeOptions);

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
         
    driver.manage().window().maximize();
            driver.get(applicationName);
}

@When("^Enter valid \"([^\"]*)\" and valid \"([^\"]*)\"$")
public void Enter_valid_username_and_valid_password(String userName, String password) throws Throwable {
    driver.findElement(By.id("email")).sendKeys(userName);
    driver.findElement(By.id("pass")).sendKeys(password);     
}

@Then("user should be able to login successfully")
public void user_should_be_able_to_login_successfully() throws Throwable {
    driver.findElement(By.id("loginbutton")).click();
}

@Then("user should be able to logout successfully")
public void user_should_be_able_to_logout_successfully() throws Throwable {
             driver.findElement(By.id("userNavigationLabel")).click();
     driver.findElement(By.linkText("Log Out")).click();
}
}

Feature: Test Facebook smoke scenario

    Scenario Outline: Test login with valid credentials
    Given Open "webdriver.chrome.driver" and the "C:\\xyz\\chromedriver.exe" start "http://www.facebook.com" application
    When Enter valid "<userName>" and valid "<password>"
    Then user should be able to login successfully
    And user should be able to logout successfully

    Examples: 
      | userName                 | password  |
      | abc_efgh@yahoo.com | xxxx |
      |xyz@yahoo.com | yyyy



Sunday, January 21, 2018

Selenium ...

Selenium Grid

* You can automate upto 5 browsers.
* You can run both Web Driver & RC.
* Only one Hub for a grid.
* Before creating the grid make sure firewall settings are correctly configured.

Setting up the Hub

G:\TestAutomation\Selenium>java -jar selenium-server-standalone-2.48.2.jar -role hub
13:51:50.635 INFO - Launching Selenium Grid hub
2018-01-22 13:51:51.626:INFO::main: Logging initialized @1291ms
13:51:51.637 INFO - Will listen on 4444
13:51:51.672 INFO - Will listen on 4444
2018-01-22 13:51:51.675:INFO:osjs.Server:main: jetty-9.2.z-SNAPSHOT
2018-01-22 13:51:51.722:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@2ddc9a9f{/,null,AVAILABLE}
2018-01-22 13:51:52.619:INFO:osjs.ServerConnector:main: Started ServerConnector@1a75e76a{HTTP/1.1}{0.0.0.0:4444}
2018-01-22 13:51:52.622:INFO:osjs.Server:main: Started @2287ms
13:51:52.624 INFO - Nodes should register to http://172.18.175.50:4444/grid/register/
13:51:52.625 INFO - Selenium Grid hub is up and running
14:00:46.294 INFO - Registered a node http://172.18.175.50:5555

Setting up/ Registering Node1

G:\TestAutomation\Selenium>java -Dwebdriver.chrome.driver=E:\Software\chromedriver.exe -jar selenium-server-standalone-2.48.2.jar -role node -hub  http://172.18.175.50:4444/grid/register/
14:00:45.605 INFO - Launching a Selenium Grid node
14:00:46.113 INFO - Java: Oracle Corporation 9.0.1+11
14:00:46.113 INFO - OS: Windows 10 10.0 amd64
14:00:46.119 INFO - v2.48.0, with Core v2.48.0. Built from revision 41bccdd
14:00:46.167 INFO - Driver class not found: com.opera.core.systems.OperaDriver
14:00:46.168 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
14:00:46.198 INFO - Version Jetty/5.1.x
14:00:46.200 INFO - Started HttpContext[/selenium-server,/selenium-server]
14:00:46.256 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@45385f75
14:00:46.256 INFO - Started HttpContext[/wd,/wd]
14:00:46.257 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
14:00:46.258 INFO - Started HttpContext[/,/]
14:00:46.263 INFO - Started SocketListener on 0.0.0.0:5555
14:00:46.263 INFO - Started org.openqa.jetty.jetty.Server@2c1b194a
14:00:46.264 INFO - Selenium Grid node is up and ready to register to the hub
14:00:46.285 INFO - Starting auto registration thread. Will try to register every 5000 ms.
14:00:46.285 INFO - Registering the node to the hub: http://172.18.175.50:4444/grid/register
14:00:46.294 INFO - The node is registered to the hub and ready to use 

Accessing grid hub from URL :

http://172.18.175.50:4444/grid/console


import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.concurrent.TimeUnit;

public class SeleniumGrid {

    WebDriver driver;   // WD   
    String nodeUrl;  // node1
   
/**     
 * WD & node01 runs in two different machines      
 */    
private void invokeRemoteBrowser() {
        try {

            nodeUrl = "http://172.18.175.50:5555/wd/hub";

            DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
            desiredCapabilities.setBrowserName("chrome");
            desiredCapabilities.setPlatform(Platform.WINDOWS);

            driver = new RemoteWebDriver(new URL(nodeUrl), desiredCapabilities);

            driver.manage().deleteAllCookies();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

            driver.get("https://www.yahoo.com");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}