Call Methods from two classes in Selenium C# -
this stupid question haven't found answer leads me solution yet
say have testmethod verify functionality of login portal. it's in testclassa. want run method in testclassb's testinitialize method can reliably have selenium start on blank slate testing features past login portal.
here's test code in question
using login_elements; using dashboard; namespace test_dashboard_elements { [testclass] public class dashboardtests { iwebdriver _driver; dashboardelements dash; [testinitialize] public void test_setup() { dash = new dashboardelements(_driver); loginpage login = new loginpage(_driver); _driver = new firefoxdriver(); _driver.navigate().gotourl("exampleurl/login"); login.login(); } } which calls instance of dashboardelements , passes selenium webdriver, calls instance of loginpage , passes selenium webdriver (which assume issue), , calls login method loginpage
iwebdriver _driver; //username field [findsby(how = how.id, using = "username")] private iwebelement username; //password field [findsby(how = how.id, using = "password")] private iwebelement password; //submit button [findsby(how = how.classname, using = "btn")] private iwebelement submit_button; //constructor public loginpage(iwebdriver driver) { this._driver = driver; pagefactory.initelements(driver, this); } //sends passed string username field public void sendusername(string strusername) { username.sendkeys(strusername); } //sends passed string password field public void sendpassword(string strpassword) { password.sendkeys(strpassword); } //clicks submit button public void submit() { submit_button.click(); } public void login() { sendusername("username"); sendpassword("password!"); submit(); } this returns
>message: initialization method test_dashboard_elements.dashboardtests.test_setup threw exception. system.argumentnullexception: system.argumentnullexception: searchcontext may not null parameter name: searchcontext i feel has passing _driver twice i'm not sure how else it
stack trace:
>test name: test_link_reports test fullname: test_dashboard_elements.dashboardtests.test_link_reports test source: c:\users\%user%\documents\visual studio 2013\projects\%path dir%\page tests\dashboard tests.cs : line 29 test outcome: failed test duration: 0:00:06.0255821 result message:
>initialization method test_dashboard_elements.dashboardtests.test_setup threw exception. system.argumentnullexception: system.argumentnullexception: searchcontext may not null parameter name: searchcontext. result stacktrace: @ openqa.selenium.support.pageobjects.defaultelementlocatorfactory.locateelement(isearchcontext searchcontext, ienumerable`1 bys) @ openqa.selenium.support.pageobjects.webelementproxy.get_wrappedelement() @ openqa.selenium.support.pageobjects.webelementproxy.sendkeys(string text) @ login_elements.loginpage.sendusername(string strusername) in c:\users\%user%\documents\visual studio 2013\projects\%path dir%\page elements\login elements.cs:line 39 @ login_elements.loginpage.login() in c:\users\%user%\documents\visual studio 2013\projects\%path dir%\page elements\login elements.cs:line 56 @ test_dashboard_elements.dashboardtests.test_setup() in c:\users\%user%\documents\visual studio 2013\projects\%path dir%\page tests\dashboard tests.cs:line 24 defaultelementlocatorfactory.locateelement(isearchcontext searchcontext, ienumerable 1 bys) webelementproxy.get_wrappedelement() webelementproxy.sendkeys(string text) loginpage.sendusername(string strusername) loginpage.login() dashboardtests.test_setup()
seems issue driver instantiation. not passing instantiated driver dashboardelements(). fix this:
- first instantiate
driver. pass instantiated
driverpageobject.using login_elements; using dashboard; namespace test_dashboard_elements { [testclass] public class dashboardtests { iwebdriver _driver; dashboardelements dash; [testinitialize] public void test_setup() { _driver = new firefoxdriver(); dash = new dashboardelements(_driver); loginpage login = new loginpage(_driver); _driver.navigate().gotourl("exampleurl/login"); login.login(); } }
Comments
Post a Comment