How Can I implement Hooks In Cucumber as a separate Class file? - selenium

This is the baseclass.
package utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
public class BaseClass {
public static WebDriver driver;
public WebDriverWait wait;
public static Properties prop;
FileInputStream fis;
static String browserName;
String currentDir = System.getProperty("user.dir");
public BaseClass()
{
prop=new Properties();
try {
fis = new FileInputStream(currentDir + "/src/test/java/config/data.properties");
} catch (FileNotFoundException e1) {
}
try {
prop.load(fis);
} catch (IOException e) {
}
}
public static void initialization()
{
browserName = prop.getProperty("browser");
if (browserName.equalsIgnoreCase("Chrome"))
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
else if(browserName.contains("Firefox"))
{
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
else
{
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
}
driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
}
}
This is login page
package Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.BaseClass;
public class LoginPage extends BaseClass {
WebDriver driver;
public LoginPage(WebDriver ldriver)
{
this.driver= ldriver;
wait = new WebDriverWait(driver, 90);
PageFactory.initElements(driver, this);
}
#FindBy(how=How.XPATH, using="(//*[#type='radio'])[1]//parent::div")
WebElement radioAccount;
#FindBy(how=How.XPATH, using="(//*[#type='radio'])[2]//parent::div")
WebElement radioVcNumber;
#FindBy(how=How.XPATH, using="(//*[#type='radio'])[3]//parent::div")
WebElement macId;
#FindBy(how=How.XPATH, using="(//*[#type='radio'])[4]//parent::div")
WebElement vSc;
#FindBy(how=How.XPATH, using="(//*[#type='radio'])[5]//parent::div")
WebElement rMn;
#FindBy(how=How.XPATH,using="//*[#id='txt_InputVal']")
WebElement enterRmn;
#FindBy(how=How.XPATH, using="//*[#id='btnsubmit']")
WebElement buttonSubmit;
public void clickRadioButtons()
{
wait.until(ExpectedConditions.visibilityOf(radioAccount));
radioAccount.click();
radioVcNumber.click();
macId.click();
vSc.click();
rMn.click();
}
public void loginWithRmn(String num)
{
rMn.click();
wait.until(ExpectedConditions.visibilityOf(enterRmn));
enterRmn.sendKeys(num);
}
public void clickOnLoginButton()
{
buttonSubmit.click();
}
}
This is step definition of login page.
package stepDefs;
import Pages.LoginPage;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import utils.BaseClass;
public class LoginStepDef extends BaseClass{
LoginPage login;
String title;
#Given("^User has launched URL on browser$")
public void user_has_launched_URL_on_browser()
{
BaseClass.initialization();
System.out.println("Browser Launched");
login = new LoginPage(driver);
}
#Given("^User is able to select different account types$")
public void user_is_able_to_select_different_account_types()
{
login = new LoginPage(driver);
login.clickRadioButtons();
}
#When("^User enters tries to login with Mobile Number \"([^\"]*)\"$")
public void user_enters_tries_to_login_with_Mobile_Number(String arg1)
{
login.loginWithRmn(arg1);
}
#Then("^User is logged in$")
public void user_is_logged_in()
{
login.clickOnLoginButton();
}
#After()
public void tearDown()
{
System.out.println("Browser Closed");
driver.quit();
}
}
This is test runner.
package runners;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features= {"src/test/java/featureFiles"},
glue= {"stepDefs"},
monochrome=true,
dryRun=false,
plugin= {"pretty"}
)
public class TestRunnerLogin {
}
How can I implement Hooks ?
Hooks as a different class file(Hooks.java) so that I don't have to write invoke browser again and again for further pages.
I want Hooks.java as a separate class and not implemented in some step definition.

this is my HOOKS file. maybe this can help you:
public class Hooks {
public static WebDriver driver;
#Before
public void startTest(Scenario scenario) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/mac/chromedriver");
driver = new ChromeDriver();
driver.get("http://google.com");
}
#After
public void tearDown(Scenario scenario) {
Helper.screenshot(scenario);
driver.close();
}
public static WebDriver getDriver() { 
return driver;
}
}
and follow how to use:
public class LoginSteps {
private LoginPage loginPage = new LoginPage(Hooks.getDriver());
#When("something")
public void signCheck()  {
Assert.assertTrue("Login", homePage.checkPage());
}
}

Related

Where the property key name for the chromedriver has been determined and where it is getting utilized in Selenium?

We usually set the system property for the chromedriver exe using the below code
System.setProperty("webdriver.chrome.driver",path of the chromedriver.exe)
How the property key name is determined as "webdriver.chrome.driver"?
Where the property is utilized in Webdriver wire protocol(Any particular class file for reference)?
Hi i have write utils class for that
You can open browser/navigate url/close browser and etc
package utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
public class TestApp {
private WebDriver driver;
private static TestApp myObj;
// public static WebDriver driver;
utils.PropertyFileReader property = new PropertyFileReader();
public static TestApp getInstance() {
if (myObj == null) {
myObj = new TestApp();
return myObj;
} else {
return myObj;
}
}
//get the selenium driver
public WebDriver getDriver()
{
return driver;
}
//when selenium opens the browsers it will automatically set the web driver
private void setDriver(WebDriver driver) {
this.driver = driver;
}
public static void setMyObj(TestApp myObj) {
TestApp.myObj = myObj;
}
public void openBrowser() {
//String chromeDriverPath = property.getProperty("config", "chrome.driver.path");
//String chromeDriverPath = property.getProperty("config", getChromeDriverFilePath());
System.setProperty("webdriver.chrome.driver", getChromeDriverFilePath());
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void navigateToURL() {
String url =property.getProperty("config","url");
driver.get(url);
}
public void closeBrowser()
{
driver.quit();
}
public WebElement waitForElement(By locator, int timeout)
{
WebElement element = new WebDriverWait(TestApp.getInstance().getDriver(), timeout).until
(ExpectedConditions.presenceOfElementLocated(locator));
return element;
}
private String getChromeDriverFilePath()
{
// checking resources file for chrome driver in side main resources
URL res = getClass().getClassLoader().getResource("chromedriver.exe");
File file = null;
try {
file = Paths.get(res.toURI()).toFile();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
}
enter code here

Getting NullPointerException when trying to access Selenium Webdriver instance in below scenario.

I am trying to create a framework where I can run my test cases(test tags in xml file) in parallel in different browsers. I have tried using all combinations of testNG annotations but after reading a blog came to know that this can only be achieved by using testNg listners. I am using ThreadLocal to keep my driver thread-safe. I am getting null pointer exception when trying to access Webdriver in my test cases class in line LocalDriverManager.getDriver().get(url);
This is LocalBrowserFactory.Class
package BrowserFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import DataProvider.ConfigDataProvider;
public class LocalDriverFactory {
public static WebDriver createInstance(String browserName) {
WebDriver driver = null;
ConfigDataProvider config=new ConfigDataProvider();
if (browserName.toLowerCase().contains("firefox")) {
System.setProperty("webdriver.gecko.driver", config.getgeckoPath());
driver = new FirefoxDriver();
}
if (browserName.toLowerCase().contains("internet")) {
driver = new InternetExplorerDriver();
}
if (browserName.toLowerCase().contains("chrome")) {
System.setProperty("webdriver.chrome.driver", config.getchromePath());
driver = new ChromeDriver();
}
driver.manage().window().maximize();
return driver;
}
}
This is my LocalDriverManager.class
package BrowserFactory;
import org.openqa.selenium.WebDriver;
public class LocalDriverManager {
private static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
public static WebDriver getDriver() {
return webDriver.get();
}
public static void setWebDriver(WebDriver driver) {
webDriver.set(driver);
}
}
This is my ConfigPropertyReader.class
package DataProvider;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
public class ConfigDataProvider {
static Properties p;
public ConfigDataProvider()
{
File f =new File("C:/Eclipse/parallelframework/configuration/config.properties");
try {
FileInputStream fis = new FileInputStream(f);
p=new Properties();
p.load(fis);
}
catch (Exception e)
{
System.out.println("Custom Exception- cannot load properties file"+e.getMessage());
}
}
public String getvalue(String key)
{
return p.getProperty(key);
}
public String getUrl()
{
return p.getProperty("url");
}
public String getchromePath()
{
return p.getProperty("chromepath");
}
public String getgeckoPath()
{
return p.getProperty("firefoxpath");
}
public String getIEPath()
{
return System.getProperty("User.dir")+p.getProperty("IEPath");
}
}
This is WebDriverListner.class
package Listners;
import org.openqa.selenium.WebDriver;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
import BrowserFactory.LocalDriverFactory;
import BrowserFactory.LocalDriverManager;
public class WebDriverListner implements IInvokedMethodListener {
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.isTestMethod()) {
String browserName = method.getTestMethod().getXmlTest().getLocalParameters().get("browserName");
System.out.println(browserName);
WebDriver driver = LocalDriverFactory.createInstance(browserName);
LocalDriverManager.setWebDriver(driver);
System.out.println("Driver Set");
}
}
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.isTestMethod()) {
WebDriver driver = LocalDriverManager.getDriver();
if (driver != null) {
driver.quit();
}
}
}
}
This is my sample testcase.class
package tests;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import BrowserFactory.LocalDriverManager;
public class sampleTest {
#Test
public void testMethod1() {
invokeBrowser("http://www.ndtv.com");
}
#Test
public void testMethod2() {
invokeBrowser("http://www.facebook.com");
}
private void invokeBrowser(String url) {
LocalDriverManager.getDriver().get(url);
}
}
Your Listener code is messed up. You basically have the beforeInvocation and afterInvocation in the wrong order.
Here's how it should have looked like
import org.openqa.selenium.WebDriver;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
public class WebDriverListener implements IInvokedMethodListener {
#Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.isTestMethod()) {
String browserName = method.getTestMethod().getXmlTest().getLocalParameters().get("browserName");
WebDriver driver = LocalDriverFactory.createInstance(browserName);
LocalDriverManager.setWebDriver(driver);
}
}
#Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.isTestMethod()) {
WebDriver driver = LocalDriverManager.getDriver();
if (driver != null) {
driver.quit();
}
}
}
}
You basically would setup the webdriver into the thread local variable (push the webdriver instance into the thread local variable's context) from within the beforeInvocation and then within your afterInvocation method, you would pop it out and clean up the webdriver instance (by calling quit() ). In your case, your code is doing the opposite.
For more details you can refer to my blog post here.

how to remove error "Cannot instantiate class day2practice.day2" in testng class

package day2practice;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
public class day2 {
ChromeDriver driver = new ChromeDriver();
#BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Ashok\\Desktop\\chromedriver.exe");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.navigate().to("http://live.guru99.com");
}
#Test
public void f() {
String actual = driver.getTitle();
String expected = "THIS IS DEMO SITE";
Assert.assertEquals(actual, expected, "page title is same");
}
#AfterMethod
public void close() {
driver.quit();
}
}
Don't instantiate ChromeDriver class before assigning the path to chromedriver.exe. Moved the instantiation code to beforeMethod.
try the following code:
package day2practice;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class day2 {
ChromeDriver driver;
#BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Ashok\\Desktop\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.navigate().to("http://live.guru99.com");
}
#Test
public void f() {
String actual = driver.getTitle();
String expected = "THIS IS DEMO SITE";
Assert.assertEquals(actual, expected, "page title is same");
}
#AfterMethod
public void close() {
driver.quit();
}
}

Can't able to call method from multiple classes in selenium webdriver

I am trying access method from two classes in another class but only one class method is called. during call of another class method it gives NullpointerException error. Please give me solution.
Code is here--->
Setup class-->
package BasePOI;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Setup {
public WebDriver driver;
public void Websiteopen() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("url");
}
public Setup(WebDriver driver){
this.driver=driver;
}
}
Login page object class--->
package BasePOI;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPOI {
public WebDriver driver;
//home
By home_login=By.linkText("Login");
By about_us=By.linkText("About Us");
//login
By counselor=By.id("counselor_login");
By user=By.id("user_login");
By username=By.id("username");
By password=By.id("password");
By Login=By.name("Login");
By create_account=By.name("Login");
By Logout=By.linkText("Logout");
public LoginPOI(WebDriver driver){
this.driver=driver;
}
public void click_Login_button(){
try {
driver.findElement(home_login).click();
}
catch (Exception e)
{
System.out.println(e);
}
}
public void click_Login_counselor(){
driver.findElement(counselor).click();
}
public void click_Login_user(){
driver.findElement(user).click();
}
public void Enter_login_data(String uname,String pwd){
driver.findElement(username).clear();
driver.findElement(username).sendKeys(uname);
driver.findElement(password).clear();
driver.findElement(password).sendKeys(pwd);
}
public void click_Login(){
driver.findElement(Login).click();
}
}
Now i am calling both classes method in another class
Login functionlity class--->
package Functionlity;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import BasePOI.LoginPOI;
import BasePOI.Setup;
public class Login {
public WebDriver driver;
#Test
public void openwebsite() throws InterruptedException{
Setup a= new Setup(driver);
a.Websiteopen();
Thread.sleep(10000);
LoginPOI b=new LoginPOI(driver);
b.click_Login_button();
}
}
Here website method is running but click_Login_button method gives me
errorerror--->
java.lang.NullPointerException
The Error is because the driver under LoginPOI class is not initialized. Change your code as per below and try -
Setup Class
package BasePOI;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Setup {
public static WebDriver driver;
public void Websiteopen()
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("url");
}
public Setup(WebDriver driver)
{
this.driver=driver;
}
public Setup()
{
}
public WebDriver getDriver()
{
return this.driver;
}
}
Login POI
package BasePOI;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPOI
{
public WebDriver driver;
//home
By home_login=By.linkText("Login");
By about_us=By.linkText("About Us");
//login
By counselor=By.id("counselor_login");
By user=By.id("user_login");
By username=By.id("username");
By password=By.id("password");
By Login=By.name("Login");
By create_account=By.name("Login");
By Logout=By.linkText("Logout");
public void click_Login_button(){
try {
this.driver=new Setup().getDriver();
driver.findElement(home_login).click();
}
catch (Exception e)
{
System.out.println(e);
}
}
public void click_Login_counselor()
{
driver.findElement(counselor).click();
}
public void click_Login_user()
{
driver.findElement(user).click();
}
public void Enter_login_data(String uname,String pwd)
{
driver.findElement(username).clear();
driver.findElement(username).sendKeys(uname);
driver.findElement(password).clear();
driver.findElement(password).sendKeys(pwd);
}
public void click_Login()
{
driver.findElement(Login).click();
}
}
Login Class
package Functionlity;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Login
{
public WebDriver driver;
#Test
public void openwebsite() throws InterruptedException
{
Setup a= new Setup(driver);
a.Websiteopen();
LoginPOI b=new LoginPOI();
b.click_Login_button();
}
}
Explanation :
First need to make webderiver static under Setup class to make same driver accessible to different instances
create one default constructor in setup class and one method which return driver to access in another classes
this.driver=new Setup().getDriver(); will get the (initialized driver instances in setup class) driver in LoginPOI class
You are not initializing the driver object in your test class and that's why it throws null pointer exception when calling any webdriver method inside Page class.
The simple solution would be to modify your setup class and initialize the webdriver in setup method
Setup class
public class Setup {
public Webdriver getDriver() {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("url");
}
public Setup(WebDriver driver){
this.driver=driver;
}}
Test Class
public class Login {
public WebDriver driver;
#BeforeMethod
public void setup() {
driver = new FirefoxDriver();
}
#Test
public void openwebsite() throws InterruptedException{
Setup a = new Setup(driver);
a.Websiteopen();
LoginPOI b=new LoginPOI(driver);
b.click_Login_button();
}}

JUnit Parallel Testing

I'm trying to run a piece of code that I found online to run JUnit test in parallel but the only thing I've modified is the URL to hit my local host. Problem is I'm constantly getting the following error and even though I've setup selenium grid many times and know how to set paths etc. I don't know how to fix this one.
org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver.
here's the code
package AutomationFrameWork;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.runners.Parameterized;
import org.junit.runners.model.RunnerScheduler;
public class Parallelized extends Parameterized {
private static class ThreadPoolScheduler implements RunnerScheduler {
private ExecutorService executor;
public ThreadPoolScheduler() {
String threads = System.getProperty("junit.parallel.threads", "16");
int numThreads = Integer.parseInt(threads);
executor = Executors.newFixedThreadPool(numThreads);
}
#Override
public void finished() {
executor.shutdown();
try {
executor.awaitTermination(10, TimeUnit.MINUTES);
} catch (InterruptedException exc) {
throw new RuntimeException(exc);
}
}
#Override
public void schedule(Runnable childStatement) {
executor.submit(childStatement);
}
}
public Parallelized(Class<?> klass) throws Throwable {
super(klass);
setScheduler(new ThreadPoolScheduler());
}
}
package AutomationFrameWork;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
#RunWith(Parallelized.class)
public class JUnitParallel {
private String platform;
private String browserName;
private String browserVersion;
private WebDriver driver;
#Parameterized.Parameters
public static LinkedList<String[]> getEnvironments() throws Exception {
LinkedList<String[]> env = new LinkedList<String[]>();
env.add(new String[]{Platform.WINDOWS.toString(), "chrome", "27"});
env.add(new String[]{Platform.WINDOWS.toString(),"firefox","20"});
env.add(new String[]{Platform.WINDOWS.toString(),"ie","11"});
env.add(new String[]{Platform.WINDOWS.toString(),"safari","5.1.7"});
//add more browsers here
return env;
}
public JUnitParallel(String platform, String browserName, String browserVersion) {
this.platform = platform;
this.browserName = browserName;
this.browserVersion = browserVersion;
}
#Before
public void setUp() throws Exception {
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability("platform", platform);
capability.setCapability("browser", browserName);
capability.setCapability("browserVersion", browserVersion);
capability.setCapability("build", "JUnit-Parallel");
capability.setCapability("InternetExplorerDriverServer.IE_ENSURE_CLEAN_SESSION",true);
System.setProperty("webdriver.ie.driver",
"C:\\Users\\path to driver\\IEDriverServer.exe");
driver = new RemoteWebDriver(
new URL("http://localhost:7777".concat("/wd/hub")),
capability
);
}
#Test
public void testSimple() throws Exception {
driver.get("http://www.google.com");
String title = driver.getTitle();
System.out.println("Page title is: " + title);
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
driver = new Augmenter().augment(driver);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcFile, new File("Screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}