简体   繁体   中英

How to stop tests in selenium using webdriver (JavaScript)?

I am running two tests using selenium webdriver in nodejs. I want first test to stop and then run the second one. But issues is that driver.close throws exception.

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

driver.get('https://www.website.com/');

var values = [{email:'correctuser@website.com',password:'correct'},{email:'wronguser@website.com',password:'wrong'}];


testCase(values[0].email,values[0].password);

// driver.close throws exception
driver.close();

testCase(values[1].email,values[1].password);

 function testCase(email,password){
  driver.wait(until.urlIs('https://www.website.com/'))
  driver.findElement(By.id('inputEmail')).clear();
  driver.findElement(By.id('inputEmail')).sendKeys(email);
  driver.findElement(By.id('inputPassword')).clear();
  driver.findElement(By.id('inputPassword')).sendKeys(password);
  driver.findElement(By.tagName('button')).click().then(()=>{
    driver.wait(until.elementLocated(By.className('alert-danger'))).then(() =>{
      console.log('Failed login case - success');
    }).catch((e)=>{
      console.log(e);
    });
    driver.wait(until.urlIs('https://www.website.com/home')).then(() => {
      console.log('Successful login case - success');
    }).catch((e)=>{
      console.log(e);
    });
  });

}

Error Message :

(node:1296) UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.13.6 x86_64)

Expected Result should be :

- Successful login case - success
- Failed login case - success

Don't use driver.close() , use driver.quit() .

driver.close() will close the existing Selenium window, if there isn't a window it will error. It's really designed to be used when opening multiple different browser windows.

driver.quit() is the clean up command in selenium. It will close any existing windows and then dereference the driver object. It will not error if there are no windows currently open.

That being said, it's not the driver close that is causing you problems, it's the fact that the second test case you run (the line testCase(values[1].email,values[1].password); ) is assuming that you have a browser object. You don't because you close the last window in the previous line which results in your browser object being junked.

To get rid of this error you need to call

var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

again before you call

testCase(values[1].email,values[1].password);

*EDIT*

So to give you a full block of code:

var values = [{email:'correctuser@website.com',password:'correct'},{email:'wronguser@website.com',password:'wrong'}];
var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

driver.get('https://www.website.com/');        
testCase(values[0].email,values[0].password);

driver.quit();

driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

driver.get('https://www.website.com/');
testCase(values[1].email,values[1].password);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM