0
votes

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:'[email protected]',password:'correct'},{email:'[email protected]',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
1
Why don't you have two methods for that ? If fail is one test case, success should be second test case.cruisepandey
I want to reuse same function for insertion of keys and detecting the response.Saeed Ahmad

1 Answers

0
votes

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:'[email protected]',password:'correct'},{email:'[email protected]',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);