java - Sync between error Listener and Test execution in WebDriver -


i trying implement javascript error listener on selenium tests built in java project.

in order detect console java script errors, implemented script in javascript project push console error messages @ bottom of body, element containing specific id , error message.

the error listener coded looks like:

public class errorlistener implements runnable{      webdriver driver = null;      public errorlistener (webdriver driver)     {         this.driver = driver;     }      private void listen ()     {                try          {             while(!driver.tostring().contains("null")) //chrome alive             {                         list<webelement> list = driver.findelements(by.xpath("//div[@id='jserror']"));                  if(list.size() != 0)                 {                     webelement error = list.get(0);                      string errormsg = error.gettext();                      if(errormsg != null)                      {                             reporter.log("found javascript error code: "+errormsg);                         assert.asserttrue(false); // force test failure                     }                 }             }         }catch(exception ex)          {             ex.printstacktrace();            }     }      @override     public void run()      {         listen();     } } 

which calling in test method such as:

public class tc_newaccount  {     public webdriver driver;     public thread listener; @beforemethod public void beforetest() {     reporter.log("the test has begun.");     driver = newchrome.generate(true);      errorlistener errorlog = new errorlistener(driver);     listener = new thread(errorlog);     listener.start(); } @aftermethod public void afterclass()  {     reporter.log("test has finished.");     driver.close(); }  @dataprovider public static object[][] credentials()  {     return new object[][]   { {     "id",   "password"   }}; } @test(dataprovider = "credentials") public void execute(string username, string password) throws exception  { ... whatever test } } 

the problem is, since using same instance of chrome listen specific webelement, throwing requests in parallel both methods (the test , listener), , process ends crashing because of them being @ same time , not being synced (one after another).

i realised 1 option check errors after action every time, increment lines of code, , partial solution because errors may occur in terms of milliseconds...

is there smooth solution problem?

thanks in advance!

first of have create testlistener.java file

import org.testng.itestcontext; import org.testng.itestlistener; import org.testng.itestresult;  public class testlistener implements itestlistener {  @override public void onteststart(itestresult result) {     system.out.println("started test: " + result.getname()); }  @override public void ontestsuccess(itestresult result) {     system.out.println("finished test: " + result.getname() + " :passed"); }  @override public void ontestfailure(itestresult result) {     system.out.println("finished test: " + result.getname() + " :failed"); }  @override public void ontestskipped(itestresult result) {     system.out.println("finished test: " + result.getname() + " :skipped"); }  @override public void ontestfailedbutwithinsuccesspercentage(itestresult result) {     system.out.println("finished test: " + result.getname()             + " :failed within success percentage"); }  @override public void onstart(itestcontext context) {     // todo auto-generated method stub  } @override public void onfinish(itestcontext context) {     // todo auto-generated method stub  } } 

you can customize file according . next have need mention listener in test class

@listeners(testlistener.class)  public class testclass{ @test(dataprovider = "testdata" , dataproviderclass = dataproviderutil.class)    public void test(list data) throws exception {      try{          // code here      }     catch(exception e){         log.exception(e);     }finally {         log.endtestcase();     } } } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -