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

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.

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

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

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());
}
}

NullPointerException in my code. How to deal with it

I've written my code in Java using Selenium. When I run the code, it's throwing a NullPointerException. Check the exception below
Exception in thread "main" java.lang.NullPointerException
at AdminInterface.loginApplication(AdminInterface.java:17)
at AdminInterface.main(AdminInterface.java:29)
My code is as follows:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class AdminInterface {
public WebDriver driver;
public void launchApplication() throws Exception
{
System.setProperty("webdriver.ie.driver", "C:\\Users\\rprem\\Downloads\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get("https://www.gcrit.com/build3/admin/");
}
public void loginApplication(String Username, String Password)
{
driver.findElement(By.name("username")).sendKeys(Username);
driver.findElement(By.name("password")).sendKeys(Password);
driver.findElement(By.id("tbd1")).click();
}
public void closeBrowser()
{
driver.close();
}
public static void main(String[] args)
{
AdminInterface obj = new AdminInterface();
obj.loginApplication("admin", "admin#123");
}
}
You are seeing a NullPointerException because from main() you are trying to access the loginApplication() method right in the begining, which requires an active instance of the WebDriver i.e. the driver to findElement(By.name("username")); & findElement(By.name("password")); and perform sendKeys() method on the HTML DOM.
The solution would be to first access the launchApplication() method so you have an active instance of driver and IE Browser. Next you can access loginApplication() method.
Here is your working code block:
package demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Q45474353_NPE
{
public WebDriver driver;
public void launchApplication()
{
System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get("https://www.gcrit.com/build3/admin/");
}
public void loginApplication(String Username, String Password)
{
driver.findElement(By.name("username")).sendKeys(Username);
driver.findElement(By.name("password")).sendKeys(Password);
driver.findElement(By.id("tbd1")).click();
}
public void closeBrowser()
{
driver.close();
}
public static void main(String[] args)
{
Q45474353_NPE obj = new Q45474353_NPE();
obj.launchApplication();
obj.loginApplication("admin", "admin#123");
obj.closeBrowser();
}
}

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();
}}

TestNG + Selenium woes. noClassDef error, TestNG based selenium test

I've followed a lot of the guides and forum posts online but haven't had any luck getting this to work inside TestNG. It's a selenium grid based test, programmed in eclipse.
Had trouble, so used the libraries listed in the suggestion of this forum post: http://clearspace.openqa.org/message/66867
I am trying to run the suite in the testNG test plugin for eclipse (org.testng.eclipse). Also tried running the jar from command line through selenium grid to no avail.
Since I'm not a java developer, to be honest I'm not entirely sure what to look for. I've some familiarity with Java thanks to the Processing environment, but I've kind of been thrown into java/eclipse for this task and am at a bit of a loss. Anyway, any help is appreciated and thank you in advance.
Here's my code:
suite.java:
package seleniumRC;
//import com.thoughtworks.selenium.*;
//import junit.framework.*;
//import java.util.regex.Pattern;
//import static org.testng.AssertJUnit.assertTrue;
//import org.testng.annotations.AfterMethod;
//import org.testng.annotations.BeforeMethod;
//import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class suite extends Testcase1 {
#Test(groups = {"example", "firefox", "default"}, description = "test1")
public void user1() throws Throwable {
testCase1();
}
#Test(groups = {"example", "firefox", "default"}, description = "test2")
public void user2() throws Throwable {
testCase1();
}
}
The actual test case
package seleniumRC;
//import com.thoughtworks.selenium.*;
//import org.testng.annotations.*;
//import static org.testng.Assert.*;
//import com.thoughtworks.selenium.grid.demo.*;
//import junit.framework.*;
//import com.ibm.icu.*;
//import java.util.regex.Pattern;
//import static org.testng.AssertJUnit.assertTrue;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
public class Testcase1 extends SeleneseTestNgHelper {
private static int $itter = 10;
public static void main(String args[]) {
//junit.textui.TestRunner.run(Suite());
}
//public static Test Suite() {
// return new TestSuite(Testcase1.class);
//}
// public void setUp() throws Exception {
// setUp("http://localhost:8080/test", "*firefox");
//}
#BeforeMethod(groups = {"default", "example"}, alwaysRun = true)
#Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})
protected void startSession(String seleniumHost, int seleniumPort, String browser, String webSite) throws Exception {
startSession(seleniumHost, seleniumPort, browser, webSite);
selenium.setTimeout("120000");
}
#AfterMethod(groups = {"default", "example"}, alwaysRun = true)
protected void closeSession() throws Exception {
closeSession();
}
public void testCase1() throws Exception {
selenium.open("/login.action#login");
selenium.type("userName", "foo");
selenium.type("password", "bar");
selenium.click("login");
selenium.waitForPageToLoad("30000");
selenium.click("link=test");
Thread.sleep(4000);
selenium.click("//tr[4]/td[1]/a");
if(selenium.isElementPresent("//input[#id='nextButton']") != false){
selenium.click("//div[2]/input");
}
Thread.sleep(2000);
for(int i=0; i < $itter; i++) {
if(selenium.isElementPresent("//label") != false){
selenium.click("//label");
selenium.click("submitButton");
Thread.sleep(1500);
}
else{ Thread.sleep(1500);}
}
selenium.click("//body/div[2]/div[1]/div/a");
Thread.sleep(1500);
selenium.click("//a[contains(text(),'Finish Now')]");
Thread.sleep(2000);
selenium.click("link=View Results");
Thread.sleep(30000);
selenium.click("showAllImgCaption");
Thread.sleep(12000);
selenium.click("generateTimeButton");
Thread.sleep(2000);
selenium.click("link=Logout");
selenium.waitForPageToLoad("15000");
}
}
and the SeleneseTestNGHelper class
package seleniumRC;
import java.lang.reflect.Method;
//import java.net.BindException;
import com.thoughtworks.selenium.*;
//import org.openqa.selenium.SeleniumTestEnvironment;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
//import org.openqa.selenium.environment.GlobalTestEnvironment;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
public class SeleneseTestNgHelper extends SeleneseTestCase
{
private static Selenium staticSelenium;
#BeforeTest
#Override
#Parameters({"selenium.url", "selenium.browser"})
public void setUp(#Optional String url, #Optional String browserString) throws Exception {
if (browserString == null) browserString = runtimeBrowserString();
WebDriver driver = null;
if (browserString.contains("firefox") || browserString.contains("chrome")) {
System.setProperty("webdriver.firefox.development", "true");
driver = new FirefoxDriver();
} else if (browserString.contains("ie") || browserString.contains("hta")) {
driver = new InternetExplorerDriver();
} else {
fail("Cannot determine which browser to load: " + browserString);
}
if (url == null)
url = "http://localhost:4444/selenium-server";
selenium = new WebDriverBackedSelenium(driver, url);
staticSelenium = selenium;
}
#BeforeClass
#Parameters({"selenium.restartSession"})
public void getSelenium(#Optional("false") boolean restartSession) {
selenium = staticSelenium;
if (restartSession) {
selenium.stop();
selenium.start();
}
}
#BeforeMethod
public void setTestContext(Method method) {
selenium.setContext(method.getDeclaringClass().getSimpleName() + "." + method.getName());
}
#AfterMethod
#Override
public void checkForVerificationErrors() {
super.checkForVerificationErrors();
}
#AfterMethod(alwaysRun=true)
public void selectDefaultWindow() {
if (selenium != null) selenium.selectWindow("null");
}
#AfterTest(alwaysRun=true)
#Override
public void tearDown() throws Exception {
// super.tearDown();
}
//#Override static method of super class (which assumes JUnit conventions)
public static void assertEquals(Object actual, Object expected) {
SeleneseTestBase.assertEquals(expected, actual);
}
//#Override static method of super class (which assumes JUnit conventions)
public static void assertEquals(String actual, String expected) {
SeleneseTestBase.assertEquals(expected, actual);
}
//#Override static method of super class (which assumes JUnit conventions)
public static void assertEquals(String actual, String[] expected) {
SeleneseTestBase.assertEquals(expected, actual);
}
//#Override static method of super class (which assumes JUnit conventions)
public static void assertEquals(String[] actual, String[] expected) {
SeleneseTestBase.assertEquals(expected, actual);
}
//#Override static method of super class (which assumes JUnit conventions)
public static boolean seleniumEquals(Object actual, Object expected) {
return SeleneseTestBase.seleniumEquals(expected, actual);
}
//#Override static method of super class (which assumes JUnit conventions)
public static boolean seleniumEquals(String actual, String expected) {
return SeleneseTestBase.seleniumEquals(expected, actual);
}
#Override
public void verifyEquals(Object actual, Object expected) {
super.verifyEquals(expected, actual);
}
#Override
public void verifyEquals(String[] actual, String[] expected) {
super.verifyEquals(expected, actual);
}
}
So I solved this by dropping the seleniumTestNGHelper class and reworking the classpaths by way of the ant file. It required completely working my suite/original testcases, but worked well within Grid.