How to setup login method to avoid duplicate code every test [selenium][java] -
i want use login method inted of repeat same code every test. check similar topic here , couldn't find solution or dont this.
test autogenerated selenium ide have more question method there. mark things confuse me. test , method made:
import java.util.regex.pattern; import java.util.concurrent.timeunit; import org.testng.annotations.*; import static org.testng.assert.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.firefoxdriver; import org.openqa.selenium.support.ui.select; public class sortowanie{ private webdriver driver; private string baseurl; private boolean acceptnextalert = true; private stringbuffer verificationerrors = new stringbuffer(); string[] params; @beforeclass(alwaysrun = true) public void setup() throws exception { driver = new firefoxdriver(); baseurl = "https://en-testwebapi.poczta-polska.pl/"; driver.manage().timeouts().implicitlywait(30, timeunit.seconds); } @test public void testsortowanie() throws exception { driver.get(baseurl + "/"); login(); driver.findelement(by.xpath("(//input[@type='search'])[2]")).clear(); driver.findelement(by.xpath("(//input[@type='search'])[2]")).sendkeys("1"); driver.findelement(by.xpath("(//input[@type='search'])[3]")).clear(); driver.findelement(by.xpath("(//input[@type='search'])[3]")).sendkeys("09"); driver.findelement(by.xpath("(//input[@type='search'])[3]")).clear(); driver.findelement(by.xpath("(//input[@type='search'])[3]")).sendkeys(""); driver.findelement(by.xpath("(//input[@type='search'])[4]")).clear(); driver.findelement(by.xpath("(//input[@type='search'])[4]")).sendkeys("21"); driver.findelement(by.xpath("(//input[@type='search'])[4]")).clear(); driver.findelement(by.xpath("(//input[@type='search'])[4]")).sendkeys(""); } @afterclass(alwaysrun = true) public void teardown() throws exception { driver.quit(); string verificationerrorstring = verificationerrors.tostring(); if (!"".equals(verificationerrorstring)) { fail(verificationerrorstring); } } //i want use params defined in run configuration public void login(){ driver.findelement(by.id("p")).clear(); driver.findelement(by.id("p")).sendkeys(params[1]); driver.findelement(by.id("u")).clear(); driver.findelement(by.id("u")).sendkeys(params[0]); driver.findelement(by.id("submit_button")).click(); } //where , when need use method ?? private boolean iselementpresent(by by) { try { driver.findelement(by); return true; } catch (nosuchelementexception e) { return false; } } //where , when need use method ?? private boolean isalertpresent() { try { driver.switchto().alert(); return true; } catch (noalertpresentexception e) { return false; } } //where , when need use method ?? private string closealertandgetitstext() { try { alert alert = driver.switchto().alert(); string alerttext = alert.gettext(); if (acceptnextalert) { alert.accept(); } else { alert.dismiss(); } return alerttext; } { acceptnextalert = true; } } }
so sake of peace repeat questions: how can make login method params value , need implement it? how work method mark in comment on code above.
any usefull article, tutorials, similar code analysis.
thank advice
you can use page factory this:
login class
package locators; import org.openqa.selenium.by; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.support.findby; import org.openqa.selenium.support.ui.expectedconditions; import org.openqa.selenium.support.ui.webdriverwait; public class login { @findby (id="p") public webelement password; @findby (id="u") public webelement user; public void login(webdriver driver, string login,string pass) { webdriverwait wait = new webdriverwait(driver, 30); wait.until(expectedconditions.elementtobeclickable(by.id("p"))); password.clear(); password.sendkeys(pass); user.clear(); user.sendkeys(login); driver.findelement(by.id("submit_button")).click(); }}
your test
import java.util.concurrent.timeunit; import org.testng.annotations.*; import static org.testng.assert.*; import org.openqa.selenium.*; import org.openqa.selenium.chrome.chromedriver; import org.openqa.selenium.support.pagefactory; import locators.login; // if test class , login class in same package there no need import public class sortowanie{ private webdriver driver; private string baseurl; private stringbuffer verificationerrors = new stringbuffer(); @beforeclass(alwaysrun = true) public void setup() throws exception { driver = new chromedriver(); // reason ff don't want load page me baseurl = "https://en-testwebapi.poczta-polska.pl/"; driver.manage().timeouts().implicitlywait(30, timeunit.seconds); } @test @parameters({"login", "password"}) public void testsortowanie(string login, string password) throws exception { login l = pagefactory.initelements(driver, login.class); // driver.get(baseurl); l.login(driver, login, password); } @afterclass(alwaysrun = true) public void teardown() throws exception { driver.quit(); string verificationerrorstring = verificationerrors.tostring(); if (!"".equals(verificationerrorstring)) { fail(verificationerrorstring); } } }
config xml file
<?xml version="1.0" encoding="utf-8"?> <!doctype suite system "http://testng.org/testng-1.0.dtd"> <suite name="sortowanie test suite" verbose="2"> <test name="sortowanie test 1" > <parameter name="login" value="login 1"/> <parameter name="password" value="pass 1"/> <classes> <class name="orders.test_setup"/> <class name="sortowanie"/> </classes> </test> <test name="sortowanie test 2" > <parameter name="login" value="login 2"/> <parameter name="password" value="pass 2"/> <classes> <class name="orders.test_setup"/> <class name="sortowanie"/> </classes> </test> </suite>
Comments
Post a Comment