Tuesday, October 25, 2016

selenium utility class

Utility class

package Library;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Utility {
   
    public static void screenshot(WebDriver driver, String screenshotName) {
       
        TakesScreenshot ts = (TakesScreenshot)driver;
               
        File source = ts.getScreenshotAs(OutputType.FILE);
               
        try {
            FileUtils.copyFile(source, new File("./screenshots/" +screenshotName+ ".png"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("exception while taking screenshot" +e.getMessage());
        }
    }
   
   

    public static void launchWebsite() {
        // TODO Auto-generated method stub
       
        System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");
        WebDriver driver1 = new ChromeDriver();
        driver1.get("http://192.168.2.1:8090/");
        driver1.findElement(By.name("username")).sendKeys("vanzauda");
        driver1.findElement(By.name("password")).sendKeys("nosmokingever");
        driver1.findElement(By.name("btnSubmit")).click();
       
       
    }

}