Category Archives: TestNG

Cloud Testing with SauceLabs:

We can set up Selenium Server on our local computer or we can use cloud grid system providers such asSauceLabs, Browserstack or TestingBot, etc. This kind of companies provides extensive grid on their servers. You can find all OS and all browser versions. In this post will see how to test with those services.

What is Sauce Labs?
SauceLabs is a grid in the cloud. We can run our tests on their cloud with different browsers and operating systems. You can also do manual testing. In order to do these, first, we should go saucelabs.com and register for free.

In the saucelabs dashboard: Automated Builds, Automated Tests, Manual Tests.

Let’s start how to Run a Selenium Tests with SauceLabs:

SauceLabs is great because they allow you to focus on testing instead of worrying about maintaining a bunch of environments against which your tests need to run. To get a small taste of this sauceomeness, let’s take a look at what it takes to get a Selenium script running in the SauceLabs Cloud.

Getting started:
1. First, we should go saucelabs.com and register for free.

Eclipse:
1. Create a Maven project and add selenium-related things (version, TestNG) in the pom.xml

In the above SampleSauceTest, We are providing “USER_NAME” and “ACCESS_KEY” to connect saucelabs.

How to get Access Key?

Sauce Labs accounts are protected by a username, a password, and an access key. We use our username and password to access Sauce Labs’ site, and we use our username and access key to allow our tests to access SuaceLabs’s Selenium servers

1. Go to User Settings
2. ACCESS_KEY – Copy and Paste it into the class.

We configure Remote WebDriver with DesiredCapabilities (it configures browser type, version, platform, etc.) and external server URL. Then, we can run our tests on the server which we configured for RemoteWebDriver.

NOTE: Run Script and Verify on Saucelabs

Overview of Sauce Labs Features:
1. Web Functional testing
2. Cross Browser testing
3. Mobile Web testing
4. Manual Testing
5. Automated Testing Platform
6. Real device coverage

Benefits of SauceLabs:
1. Running tests 24/7
2. Fewer human resources
3. Simultaneity
4. Record
5. Screenshots

Thanks for reading this article!

In the Previous Blog we Created a Maven Project.. then the Question comes what is Next??

For Reference of Maven Project Creation link : http://blog.nbostech.com/2016/08/about-maven-its-usage-and-setup-in-automation/

In this blog, I am going to discuss about the following :

  • Making Project TestNG Enabled.
  • Project Structure
  • Sample Selenium Test with TestNG annotations
  • Basic Structure of testng.xml
  • Creating profile for testng.xml in pom.xml
  • Executing Selenium test using maven
  • Viewing the Test Reports whether it failed or passed

TestNG is a testing framework for the Java programming language. It is designed to simplify a broad range of testing needs..

Few Advantages :

  • Advance and easy annotations
  • Execution patterns can be set
  • Concurrent execution of test scripts
    • Write the business logic of your test and insert TestNG annotations in your code.
    • Add the information about your test (e.g. the class name, the groups you wish to run, etc…) in a testng.xml file or in build.xml.
    •   testng.xml covers..
      • It can contain one or more tests and is defined by the <suite> tag.
      • A test is represented by <test> and can contain one or more TestNG classes.
      • A TestNG class is a Java class that contains at least one TestNG annotation. It is represented by the <class> tag and can contain one or more test methods.
      • A test method is a Java method annotated by @Test in your source.

      Frequently used annontations

      • @Test :
      • @beforeTest :
      • @AfterTest:
      • @beforeClass

To Start our Maven project with Selenium test and using TestNG.. The below are the steps to follow..

1. TestNG Enable

Make the project TestNG enabled.. we should add TestNG jar dependency details in pom.xml as below.

p2

Create testng.xml ..

   2. Project Structure

p3

    3. Sample Selenium test code along with TestNG

      AppTest.java


package com.wavelabs.webdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AppTest {
 
 public WebDriver driver;

 @BeforeTest
 public void beforeTest(){
 driver = new FirefoxDriver();
 }
 
 @Test(description = "facebook open page")
 public void url() {
 
 driver.get("https://facebook.com");

 }

 @Test(description = "login page", dependsOnMethods = "url")
 public void login() {

 driver.findElement(By.id("username")).sendKeys("**********");
 driver.findElement(By.id("password")).sendKeys("********");
 }
}

The annotations used in the above class are the basic annotations.. there are more like , we can use based on our requirement
@Test : It runs the method.. we have two methods with @Test annotation.. they run based on the parameters passed in the @Test annotation..
The parameter we are using is dependsOnMethods .. it means the method runs only after the url method is passed..

There are other parameters too.. eg : priority=1 , 2 .. etc..

@BeforeTest : This method gets executed before running of the methods who has @Test annotations.. we mostly use the method to initialize the objects of the classes.

   4. Basic Structure of testng.xml

  • test Tag :  the test. there can be multiple test tags in testng.xml
  • classes Tag: The parent tag for child tags (class tag)
  • class Tag : The class which the test tag wants to run.The structure of testng.xml looks like :
    <?xml version="1.0" encoding="UTF-8"?>
    
    <suite name=" WebDriver Suite">
    
     <test name="Testing">
     <classes>
     <class name="com.wavelabs.SeleniumTest.AppTest" />
     </classes>
     </test>
    
    </suite>
    

 

To run the test using Maven, we should create profile for testng.xml in pom.xml..

  5. Creating profile for testng.xml in pom.xml


<profiles>
 <profile>
 <id>SampleTest</id>
 <build>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.16</version>
 <configuration>
 <argLine>-Xms2048M -Xmx4096M</argLine>
 <suiteXmlFiles>
 <suiteXmlFile>src/test/java/com/wavelabs/SeleniumTest/testng.xml</suiteXmlFile>
 </suiteXmlFiles>
 </configuration>
 <executions>
 <execution>
 <phase>integration-test</phase>
 <goals>
 <goal>test</goal>
 </goals>
 </execution>
 </executions>
 </plugin>
 </plugins>
 </build>
 </profile>
 </profiles>

Note :  we can have multiple testng.xml’s based on our requirement.. but with different names.. instead of testng.xml. we can give any valid name ..

For multiple testng.xml’s we should create multiple <profile> under <profiles> tag (Parent tag )..

6.  Executing Selenium test using maven

Run the command from the path of your project..
To Execute using Maven.. Command to use is :
Syntax :
mvn test -P”ProfileID”
Example :
mvn test -PSampleTest

7. Viewing the Test Reports whether it failed or passed

In the project structure, we see a ‘target folder’ , this folder contains the reports..
The file ‘emailable-report.html‘ gives you the complete report of your test..

p4