Unleashing Selenium Java Test Automation Framework. Code Name : SelJava

Unleashing Selenium Java Test Automation Framework. Code Name : SelJava

Test automation is not long lasting or useful when we use a tool as alone. We need to use a test automation tool withing a test automation framework which will help us get its highest usefulness and returns.

Today I will be unleashing another test automation framework which I have designed and developed which is called SelJava. The test automation framework uses the following features.

  • Use of fluent style page object model
  • Using JSON based data driven
  • Use of Arquillian Spacelift to manage auto download and extraction of drivers
  • Using TestNG Metrics Report for reporting

Now let's see how we have developed framework

Using fluent style page object pattern

The test auromation framework is designed on fluent style page object pattern.

Login page -

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;


public class LoginPageObject {
	
	   private final WebDriver driver;


	    @FindBy(name = "uid")
	    private WebElement userName;


	    @FindBy(name = "password")
	    private WebElement password;


	    @FindBy(name = "btnLogin")
	    private WebElement loginBtn;
	    
	    private LoginPageObject(WebDriver driver) {
	        this.driver = driver;
	        PageFactory.initElements(driver, this);
	    }


	    public static LoginPageObject using(WebDriver driver) {
	        return new LoginPageObject(driver);
	    }
	    
	    public LoginPageObject setUserName (String userName) {
	        this.userName.sendKeys(userName);
	        return this;
	    }
	    
	    public LoginPageObject setPassword (String password) {
	        this.password.sendKeys(password);
	        return this;
	    }
	    
	    public LoginPageObject clickLoginButton () {
	        this.loginBtn.click();
	        return this;
	    }


}

Home page -

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;


public class HomePageObject {
	 private final WebDriver driver;


	    @FindBy(xpath = "https://table//tr[@class='heading3']")
	    private WebElement userNameHeader;


	 
	    
	    private HomePageObject(WebDriver driver) {
	        this.driver = driver;
	        PageFactory.initElements(driver, this);
	    }


	    public static HomePageObject using(WebDriver driver) {
	        return new HomePageObject(driver);
	    }
	    
	   
	    
	    public String getTextUserName () {
	        return  this.userNameHeader.getText();
	       
	    }
}

Next the framework uses JSON based data driven capabilities. It is developed with Google library of reading data from JSON file.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


public class JSONRead {
	JSONParser parser = new JSONParser(); 
	public String jsonRead(String filePath, String tagName) throws FileNotFoundException, IOException, ParseException
	{
		 
		 Object obj = parser.parse(new FileReader(filePath));


	      JSONObject jsonObject = (JSONObject) obj;
	       return (String) jsonObject.get(tagName);
	      
	}
}

Framework is also equipped with Arquillian Spacelift based browser driver management where downloading drivers, extracting and executing are done in an automated way. Follwing is the code snippet where I have done Fire Fox and Chrome driver management with Arquillian Spacelift.

import org.arquillian.spacelift.Spacelift;
import org.arquillian.spacelift.task.archive.UnzipTool;
import org.arquillian.spacelift.task.net.DownloadTool;


public class DeployBrowserDrivers {


	  private final static String CHROMEDRIVER_URL = "https://chromedriver.storage.googleapis.com/76.0.3809.68/chromedriver_win32.zip";


	  private final static String GEKHODRIVER_URL = "https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-win32.zip";


	    public static void setupChrome() {


	        Spacelift.task(DownloadTool.class)
	            .from(CHROMEDRIVER_URL)
	            .to(System.getProperty("user.dir") + "/chrome.zip")
	            .then(UnzipTool.class)
	            .toDir(System.getProperty("user.dir") + "/chrome/")
	            .execute()
	            .await();


	        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/chrome/chromedriver.exe");


	    }
	    


	    public static void setupFireFox() {


	        Spacelift.task(DownloadTool.class)
	            .from(GEKHODRIVER_URL)
	            .to(System.getProperty("user.dir") + "/FF.zip")
	            .then(UnzipTool.class)
	            .toDir(System.getProperty("user.dir") + "/FF_DRIVER2/")
	            .execute()
	            .await();


	        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/FF_DRIVER2/geckodriver.exe");


	    }
}

The framework uses TestNG metrics report to generate reports and there is no need to give explicit assertions as it is using the AssertJ based assertions to generate the passes and failures.

No alt text provided for this image

The framework uses fluent style assertions by integrating AssertJ assertions.

  assertThat(HomePageObject.using(driver).getTextUserName())
                .startsWith("Manger")
			    .contains("Manger")
			    .endsWith("123");

I have integrated the following dependencies to SelJava framework.

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Selenium01</groupId>
  <artifactId>Selenium01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
  </dependency>
<dependency>
   <groupId>org.arquillian.spacelift</groupId>
   <artifactId>arquillian-spacelift</artifactId>
   <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.9.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
  <groupId>com.github.adiralashiva8</groupId>
  <artifactId>testng-metrics</artifactId>
  <version>1.5</version>
</dependency>
  </dependencies>
</project>

Folder structure of the framework

No alt text provided for this image

Learn more about creating awesome automation frameworks as such by joining in to the test automation framework design and development program carried out globally.

Git Hub Link -





要查看或添加评论,请登录

Kushan Shalindra Amarasiri的更多文章

社区洞察

其他会员也浏览了