Showing posts with label Selenium with Java. Show all posts
Showing posts with label Selenium with Java. Show all posts

Thursday, April 26, 2018

Headless Browser : Run test in headless browser


Problem Statement: Your scripts execution takes a lot of time and you want to reduce this execution time.

How: Headless browser is a program with no UI but works as any other normal browser in the background.

Overall Benefits:
1. To improve the overall speed of script execution.
2. Opening multiple browsers on one single machine. Since there is no UI so less memory is used. can be used for load testing.
3. If you cant install a browser on a machine, a headless browser is the best option to choose.
4. Your machine is free for other tasks to do.

Options:
Few headless drivers are:
1. Chrome
2. HTMLUnit
3. Ghost
4. PhantomJS
5. Watir
6. Slimer
7. Trifle

How to do it with chrome:

Initialise your browser withthe following setup:

System.setProperty("webdriver.chrome.driver", "ChromeDriverPath");
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
WebDriver driver = new ChromeDriver(options);

And run your scripts

Notes: Google support headless browser with version 59 and above.

Wednesday, February 21, 2018

Integrate Jenkins with Selenium, Java and Maven

We all know the purpose of Jenkins but how to integrate it with a selenium project which uses Java and Maven.

Prerequisite: Install Java, Maven, and Jenkins in the system where test case execution needs to be performed.

Step 1: Install few plugin in Jenkins shown in the pic below.



Step 2: Goto "Manage Jenkins" -> "Global Tool Configuration" and configure Java and Maven. setup the path of JAVA_HOME and MAVEN_HOME as per your system configuration. 

Step 3: Goto Jenkins -> New Item -> Give a name to this project like "DummyAutomation" -> Select "Freestyle project" and click OK.

Step 4: In this new DummyAutomation Project, Goto "Build" Section and click "Add Build Step" and select "Invoke top-level Maven targets".

Step 5: Select the "Maven Version", given in step 2. Set the goal "clean install" and set the path of POM file as shown in the pic below. 


We are done with the basic set up of Jenkins.

Step 6:  Save the project and click on "Build Now". Project will execute with a success


There are certain other advance areas that you can touch and make your Jenkins setup more reliable

Step 7: you can setup the email notification by selection "Editable Email Notification" in "Post-build Actions".




Grouping the tests and setting the priorities using TestNG annotation

Usually, a tester needs to execute the tests in a group as per requirement and in that group, tests are required to execute in a predefined order because of their dependency on each other.

TestNG gives us the facility to perform these actions with very easy to use annotation. Let's try to learn with an easy example-

Step 1 : Create a java test file with following code:

import org.testng.annotations.Test;

public class TestDummy1 extends TestBase{

@Test (groups = { "Sanity" })
public void Test1() throws Exception
{
System.out.println("Dummy Test 1 is called in Sanity group");
}
@Test (groups = { "Sanity" })
public void Test2() throws Exception
{
System.out.println("Dummy Test 2 is called in Sanity group");
}
@Test (groups = { "Regression" }, priority=2)
public void Test3() throws Exception
{
System.out.println("Dummy Test 3 is called in Regression group");
}
@Test (groups = { "Regression" }, priority=1)
public void Test4() throws Exception
{
System.out.println("Dummy Test 4 is called in Regression group");
}
@Test (groups = { "Regression" }, priority=3)
public void Test5() throws Exception
{
System.out.println("Dummy Test 5 is called in Regression group");
}
}

Step 2 : Create a testNG file with following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!-- <suite name="Suite" > -->
<suite name="Suite">
<listeners>
<listener class-name="com.listeners.TestNGListeners" />
</listeners>

<!-- <test name="DummyTest" parallel="tests">
<groups>
<run>
<include name="Sanity" />
</run>
</groups>
<classes>
<class name="com.tests.Dummy.TestDummy1" />
</classes>
</test> -->

<test name="DummyTest1" parallel="tests">
<groups>
<run>
<include name="Regression" />
</run>
</groups>
<classes>
<class name="com.tests.Dummy.TestDummy1" />
</classes>
</test>
</suite

Step 3: Execute the testNG file and you will get the result. Regression group will be executed as per the priority given in test methods.

Dummy Test 4 is called in Regression group
Dummy Test 3 is called in Regression group
Dummy Test 5 is called in Regression group

Step 4: Uncomment the other group(Sanity) of tests in testNG file and execute it again.

Dummy Test 1 is called in Sanity group
Dummy Test 2 is called in Sanity group

Tuesday, January 16, 2018

Powershell command to change properties file



1.   To update App.properties file with Key=Value pair



$data =[io.file]::ReadAllText("C:\Resources\ConfigFiles\app.properties")
$data = $data -replace "Environment=(.+)","Environment=RC"
[IO.File]::WriteAllText("C:\Resources\ConfigFiles\app.properties", $data.TrimEnd())




2. To update XML file with different section and Key=Value pair



[xml] $xml = Get-Content "C:\Resources\ConfigFiles\app.properties"
$($xml.configuration.appSettings.add | Where-Object { $_.key -eq "Environment" }).value = "RC"

$xml.Save("C:\Resources\ConfigFiles\app.properties") 


Thursday, April 6, 2017

Parallel execution of scripts with TestNG

Parallel execution of scripts with TestNG


TestNG gives us the facility to execute multiple tests in parallel and two different methods of doing it are:

Method 1: Having multiple test method in one class file

Step 1: Create a class file with name “DummyTest1” and add following code in it.

public class DummyTest1

{

       public WebDriver driver;

  @Test
  public void main()
  {
     driver.findElement(By.name("userName")).sendKeys("mercury");
     driver.findElement(By.name("password")).sendKeys("mercury");
     driver.findElement(By.name("login")).click();
  }
 
  @Test
  public void main1()
  {
     driver.findElement(By.name("userName")).sendKeys("mercury");
     driver.findElement(By.name("password")).sendKeys("mercury");
     driver.findElement(By.name("login")).click();
  }

  @BeforeMethod

  public void beforeMethod()
  {
      driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.get("http://newtours.demoaut.com/");
  }

  @AfterMethod

  public void afterMethod()
  {
       driver.quit();
  }

}

Step 2: Add following code in testng.xml file. Make sure you add the right class name.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
  <test name="Regression 1">
    <classes>
      <class name="AutomationFrameworkForDummy.DummyTest1"/>
    </classes>
  </test>
</suite>


Step 3: Run it and you will see multiple browser window in parallel.

Method 2:  Having two test method in two class file.

Step 1: Create a class file and add following code in it.

public class Chrome_Search {
    WebDriver driver;
    
    @BeforeTest
       @Parameters("browser2")
     public void test1(String browser2)
     {
              if(browser2.equalsIgnoreCase("chrome"))
              {
                     System.setProperty("webdriver.chrome.driver", "E://Rajesh Document//AutomationFrameworks//WordPressAutomation//Drivers//chromedriver.exe");
                       driver =new ChromeDriver();
                       driver.get("www.google.com");
                      
                 } 
              else
              {
                     System.out.println("Google Chrome"); 
              }
         }
  
   }

Step 2: Create another class file and add following code in that class file.

public class Mozilla_OpenAmazon {
    WebDriver driver;
       @BeforeTest
       @Parameters("browser1")
    public void launchbrowser(String browser1)
     {
              if(browser1.equalsIgnoreCase("firefox"))//For Firefox we dont need to download any jar file as we have Inbuilt firefox jar files
              {
                     driver=new FirefoxDriver(); 
                  System.out.println("Hello Google..."); 
                  driver.get("https://www.amazon.com/");
                  driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Dolly");
                                                  
           }
              else
              {
                     System.out.println("Firefox"); 
              }
     }
              @AfterTest
              public void closeBrowser()
              {
                     driver.quit();
              }
}

Step 3: Add following code in testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  <suite name="Suite" parallel="tests">

  <test name="FirefoxTest" >
  <parameter name = "browser1" value="Firefox"/>
  <classes>
  <class name ="AutomationFrameworkForDummy.Mozilla_OpenAmazon"> </class>
  </classes>
  </test>
   
 
  <test name ="Chrome Test">
    <parameter name = "browser2" value="Chrome"/>
   <classes>
  <class name ="AutomationFrameworkForDummy.Chrome_Search"/>
  </classes>
   </test>
   </suite>


Step 4: Now Run it.

Tuesday, April 4, 2017

Installing TestNG in Eclipse and creating first Test

1)       Launch Eclipse > On the menu bar, click Help > Choose the "Eclipse Marketplace..." option 

2)       In the Eclipse Marketplace dialog box, type TestNG in the search box and press the search button
















3)       Click on Install > A new window will open to confirm the options. Don’t make any change and click “Confirm”


4)       Click next on every other window. Accept the license. Restart the eclipse. After the restart, right-click on the project name and check the following two options.










5) Now click “Create TestNG class”to add a new class. And copy the following code

public class DummyTest1

{

   public WebDriver driver;

  @Test

  public void main()
  {
     driver.findElement(By.name("userName")).sendKeys("mercury");
     driver.findElement(By.name("password")).sendKeys("mercury");
     driver.findElement(By.name("login")).click();
  }

  @BeforeMethod

  public void beforeMethod()
  {
      driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.get("http://newtours.demoaut.com/");
  }

  @AfterMethod

  public void afterMethod()
  {
       driver.quit();
  }

}

6)       Now click Run > Run as > TestNG Test