selenium webdriver - Error when executing testng.xml using pagefactory -
i running 2 classes using testng.first class running second class failing error:
"org.testng.testngexception:can't invoke public void testscripts.newaccountcreation.accountcreation1(): either make static or add no-args constructor class"
if add non argument constructor getting null pointer exception.
i using pagefactory design test cases.
eclipse version:kepler
testng:6.9.9
chrome version: 57.0.2987.133
chrome driver: 2.27
any suggestions appreciated.
please find below code:
page factory code login page:
/** * */ package pages; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.support.findby; import pages.loginpage; public class loginpage { webdriver driver; public loginpage(webdriver ldriver) { this.driver = ldriver; } @findby(xpath = "//input[contains(@id,'username-inputel')]") public webelement username; @findby(xpath = "//input[contains(@id,'password-inputel')]") public webelement password; @findby(xpath = "//span[contains(@id,'submit-btninnerel')]") public webelement loginbutton; // methods perform actions public void pclogin(string username, string password, webdriver driver) { username.sendkeys(username); password.sendkeys(password); loginbutton.click(); } }
page factory code account creation page:
package pages; import java.util.concurrent.timeunit; import org.openqa.selenium.keys; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.interactions.actions; import org.openqa.selenium.support.findby; import org.openqa.selenium.support.ui.expectedconditions; import org.openqa.selenium.support.ui.webdriverwait; public class accounttab { webdriver driver; public accounttab(webdriver ldriver) { this.driver = ldriver; } @findby(xpath = "//span[@id='tabbar:accounttab-btnwrap']") public webelement accountdropdown; @findby(xpath = "//span[contains(@id,'accounttab_newaccount-textel')]") public webelement newaccount_button; @findby(xpath = "//textarea[contains(@id,'globalcontactnameinputset:name-inputel')]") public webelement companyname; @findby(xpath = "//input[contains(@id,'officialiddv_input-inputel')]") public webelement feinnumber; @findby(xpath = "//input[contains(@id,'globaladdressinputset:city-inputel')]") public webelement city; @findby(xpath = "//input[contains(@id,'globaladdressinputset:postalcode-inputel')]") public webelement zipcode; @findby(xpath = "//a[contains(@id,'searchlinksinputset:search')]") public webelement accountsearchbutton; @findby(xpath = "//span[@class='x-btn-inner x-btn-inner-center'][contains(@id,'newaccountbutton-btninnerel')]") public webelement createnewbutton; @findby(css = "input[id='tabbar:accounttab:accounttab_accountnumbersearchitem-inputel']") public webelement accountsearch; @findby(xpath = "//tbody[contains(@id,'gridview')]//tr[1]//td[2]") public webelement selectaccountnumber; public void accountmousehover(webdriver driver) { webdriverwait wait1 = new webdriverwait(driver, 10); wait1.until(expectedconditions.visibilityof(accountdropdown)); actions builder2 = new actions(driver); builder2.movetoelement(accountdropdown).movebyoffset(50, 0).click() .build().perform(); system.out.println("dropdown opened"); } public void accountsearch(string accountname, webdriver driver) { webdriverwait wait = new webdriverwait(driver, 20); wait.until(expectedconditions .visibilityof(newaccount_button)); driver.manage().window().maximize(); actions builder1 = new actions(driver); builder1.movetoelement(newaccount_button).click().build().perform(); system.out.println("clicked on new accounr button"); driver.manage().timeouts().implicitlywait(10, timeunit.seconds); companyname.sendkeys(accountname); try { accountsearchbutton.click(); } catch (exception e) { system.out .println("please enter 1 of minimum required fields: company name, fein" + e); throw (e); } wait.until(expectedconditions .visibilityof(createnewbutton)); createnewbutton.click(); } }
test scripts1: loginclass:
**
package testscripts; import java.util.concurrent.timeunit; import org.testng.annotations.beforesuite; import org.testng.annotations.test; import org.testng.annotations.beforemethod; import org.testng.annotations.test; import org.testng.annotations.beforemethod; import org.openqa.selenium.webdriver; import org.openqa.selenium.support.pagefactory; import pages.loginpage; import utility.configreader; import utility.genericmethods; public class login { webdriver driver; loginpage login_page; @beforesuite public void setup() { configreader cr = new configreader( "h://selenium//selenium_odsregression//testdata//config.properties"); driver = genericmethods.startbrowser("chrome", cr.geturl()); login_page = pagefactory.initelements(driver, loginpage.class); } @test public void pclogin() { login_page.pclogin("su", "gw", driver); driver.manage().timeouts().implicitlywait(10, timeunit.seconds); } }
**
test script2: accountcreation class:
package testscripts; import org.testng.annotations.beforemethod; import org.testng.annotations.test; import org.openqa.selenium.by; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.support.pagefactory; import pages.accountfilesummary; import pages.accounttab; import pages.createnewaccount; public class newaccountcreation { webdriver driver; accounttab account_tab1; createnewaccount newaccount1; accountfilesummary afs1; public newaccountcreation(webdriver ldriver) { this.driver = ldriver; account_tab1 = pagefactory.initelements(driver, accounttab.class); } @test public void accountcreation1() { account_tab1.accountmousehover(driver); account_tab1.accountsearch("webdriver_test1", driver); } }
driver class:
package utility; import java.io.file; import java.util.concurrent.timeunit; import org.apache.commons.io.fileutils; import org.openqa.selenium.outputtype; import org.openqa.selenium.takesscreenshot; import org.openqa.selenium.webdriver; import org.openqa.selenium.chrome.chromedriver; import org.openqa.selenium.chrome.chromeoptions; import org.openqa.selenium.ie.internetexplorerdriver; import utility.configreader; public class genericmethods { public static webdriver driver = null; static configreader cr = new configreader( "h://selenium//selenium_odsregression//testdata//config.properties"); public static webdriver startbrowser(string browsername, string url) { if (browsername.equalsignorecase("chrome")) { system.setproperty("webdriver.chrome.driver", cr.getchromedriverpath()); chromeoptions options = new chromeoptions(); options.addarguments("test-type"); options.addarguments("start-maximized"); options.addarguments("--js-flags=--expose-gc"); options.addarguments("--enable-precise-memory-info"); options.addarguments("--disable-popup-blocking"); options.addarguments("--disable-default-apps"); options.addarguments("test-type=browser"); options.addarguments("disable-infobars"); driver = new chromedriver(options); driver.manage().timeouts().implicitlywait(10, timeunit.seconds); } else if (browsername.equalsignorecase("ie")) { system.setproperty("webdriver.ie.driver", cr.getiedriverpath()); driver = new internetexplorerdriver(); driver.manage().window().maximize(); } driver.get(url); return driver; } }
property file:
pcurl = http://biltipolicycenter.thehartford.com/pc/policycenter.do chromebrowserpath = c://selenium//chromedriver 2.27//chromedriver.exe iebrowserpath = c://selenium//iedriverserver x86 2.53//iedriverserver.exe
testng.xml
<?xml version="1.0" encoding="utf-8"?> <!doctype suite system "http://testng.org/testng-1.0.dtd"> <suite name="suite"> <test name="test"> <classes> <class name="testscripts.login"/> <class name="testscripts.newaccountcreation"/> </classes> </test> <!-- test --> </suite> <!-- suite -->
newaccountcreation
test class because contains test method(s) , because declare class test in testng.xml
.
so, means testng must able create instance of class , that's why testng complaining.
you have 2 options:
- using a factory explain testng how create test classes (best option imo)
- using static shared driver instance (
login#driver
not yet static)
btw, organization of classes looks bit dirty , should rework them bit.
Comments
Post a Comment