java - Selenium setting up for tests -


i need login in start tests. in test class make @beforeclass method login application. start test did not work. when remove login @beforeclass , put test works fine. not want login in in test before can start them. think there problem object static not have clue why. here not working code:

public class modulestests extends functionaltest {      static loginpage loginpage;     static loginpagereceipt loginpagereceipt;      @beforeclass     public static void login() {         functionaltest.driver.get(globalvariables.url);         loginpage = new loginpage(functionaltest.driver);         loginpagereceipt = loginpage.login(globalvariables.user_name, globalvariables.password);     }      @test     public void products() {         productspage productspage = loginpagereceipt.productspage();     }     } 

here when works:

public class modulestests extends functionaltest {  @beforeclass public static void login() {     functionaltest.driver.get(globalvariables.url);         }  @test public void products() {     loginpage loginpage = new loginpage(functionaltest.driver);     loginpagereceipt loginpagereceipt = loginpage.login(globalvariables.user_name, globalvariables.password);     productspage productspage = loginpagereceipt.productspage(); }    

}

and rest of classes:

public class functionaltest {     protected static webdriver driver;      @beforeclass     public static void setup() {         chromeoptions options = new chromeoptions();         options.addarguments("--start-maximized");         system.setproperty("webdriver.chrome.driver", "src\\main\\resources\\chromedriver.exe");         driver = new chromedriver(options);         driver.manage().timeouts().implicitlywait(10, timeunit.seconds);     }      @after     public void cleanup() {         driver.manage().deleteallcookies();     }      @afterclass     public static void teardown() {         driver.close();     } }  public class loginpage extends pageobject {      @findby(id = "username")     private webelement username;      @findby(id = "password")     private webelement password;      @findby(id = "loginbutton")     private webelement loginbutton;      public loginpage(webdriver driver) {         super(driver);         asserttrue(username.isdisplayed());         asserttrue(password.isdisplayed());         asserttrue(loginbutton.isdisplayed());     }      public void enterusername(string username) {         this.username.clear();         this.username.sendkeys(username);     }      public void enteruserpassword(string password) {         this.password.clear();         this.password.sendkeys(password);     }      public loginpagereceipt login(string username, string password) {         enterusername(username);         enteruserpassword(password);         loginbutton.click();         return new loginpagereceipt(driver);     } }  public class loginpagereceipt extends pageobject {      public navigationmodules mainnav;      @findby(xpath = "//*[@id=\"loginpartial\"]")     private webelement usernamepanel;      @findby(id = "products-menuitem")     private webelement gotoproductspage;      public loginpagereceipt(webdriver driver) {         super(driver);     }      public string loginconfirmation() {         return  usernamepanel.gettext();     }      public productspage productspage() {         system.out.println(gotoproductspage.gettext());         gotoproductspage.click();         return new productspage(driver);     }      public navigationmodules initmodulenav() {         return new navigationmodules(driver);     }    }  public class productspage extends pageobject {      @findby(id = "products-content-navigation")     private webelement productmenu;      public productspage(webdriver driver) {         super(driver);         asserttrue(productmenu.isdisplayed());     } }  public class pageobject {      protected webdriver driver;      public pageobject(webdriver driver) {         this.driver = driver;         pagefactory.initelements(driver, this);         system.out.println(getclass().getname());     } } 

edit. saw when use @beforeclass method java.lang.reflect.constructor.newinstance(unknown source) exception.

you should use @before tests have login before proceeding, meanwhile, @beforeclass runs 1 time, , it's usefull in case multiple tests share same objects.

please take on link.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -