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






Tuesday, March 28, 2017

Integration of selenium Jars and first test case with Selenium

Step 1: Download web driver java client from the location.












Step 2: Extract the zip file, place all the jar files where you want to maintain your test wares.







Step 3: Launch eclipse, create a new workspace, and create a new project









Step 4: Select “Java Project”. Give a project name “AutomationForDummy”.

Step 5: Create a new package and then create a new class.





























Step 6: Add external Jars to java build path

Right click on project > select properties > Java build path > Libraries tab > click “Add External JARs”.
  • Add selenium Java jar
  • Add all other jars in libs folder













Step 7: Add following code in your class

       WebDriver driver = new FirefoxDriver();
             
       //Launch the google search engine
       driver.get("http://www.google.com");

       // Print a Log In message to the screen
       System.out.println("Successfully opened google search page");

       //Wait for 5 sec
             
       Thread.sleep(5);
                          
       // Close the driver
       driver.quit();

Step 8: Right click on the window > Run as > Java Application

See the result.