Tuesday, 12 December 2017

Adding utility yo your selenium framework part-1

Now add util classes under  test.java.util  package .

DriverFactory

This class is used to create unique instance of  webdriver .It will help to open and kill same web page for multiple test cases.

package test.java.util;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DriverFactory {

 protected static WebDriver driver = new FirefoxDriver();
 VideoReord video=new VideoReord();
 
 public DriverFactory()  {
  initialize();
  
 }

 public void initialize()  {
  if (driver == null)
   createNewDriverInstance();
  
 }

 private void createNewDriverInstance() {
  String browser = new PropertyReader().readProperty("browser");
  if (browser.equals("firefox")) {
   driver = new FirefoxDriver();
  } else {
   System.out.println("can't read browser type");
  }
 }

 public WebDriver getDriver() {
  return driver;
 }

 public void destroyDriver() throws Exception {
  driver.quit();
  driver = null;
  video.stopRecording();
 }
}
PropertyReader

This class is used to add proper respect to web browser.

package test.java.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyReader {
 Properties properties = new Properties();
 InputStream inputStream = null;

 public PropertyReader() {
  loadProperties();
 }

 private void loadProperties() {
  try {
   inputStream = new FileInputStream("/src/main/resources/config.properties");
   properties.load(inputStream);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public String readProperty(String key) {
  return properties.getProperty(key);
 }
}
Properties are set in to "/src/main/resources/config.properties"
 as
browser=firefox.

WebElementExtender

This class is used for highlighting the web elements
package test.java.util;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;


public class WebElementExtender extends DriverFactory {

 public static void highlightElement(WebElement element) {

  JavascriptExecutor js = (JavascriptExecutor) driver;
  js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);

 }

}

No comments:

Post a Comment

Set up testlink with Selenium Framework

To Integrate Testlink with selenium add following jars in your setup testlink-api-client-2.0.jar xmlrpc-common-3.1.jar xmlrpc-client-...