0

I am currently build a selenium javascript with mocha test runner & chai for validation.

I am trying to implement POM in this test.

Let me go through the dir:
src/pages/base-page.js
src/pages/login-page.js

src/test/login-test.js

login-page.js inherts from base-page.js
And test/login-test.js call class from pages/login-page.js

Here is the code pages/base-page.js

const chrome = require('selenium-webdriver/chrome')
const webdriver = require('selenium-webdriver')

let options = new chrome.Options()
options.addArguments(["--disable-blink-features=AutomationControlled", "start-maximized", "disable-infobars", "--disable-extensions"])
options.excludeSwitches('enable-logging')
let driver = new webdriver.Builder()
 .withCapabilities(webdriver.Capabilities.chrome())
 .setChromeOptions(options)
 .build()


class BasePage {
    constructor(){
        global.driver = driver;
    }
    async goToUrl(url){
        await driver.get(url);
    }
    async closeBrowser(){
        await driver.close()
        await driver.quit()
    }
};

module.exports = BasePage;

pages/login-page.js

const { By } = require('selenium-webdriver');
const { until } = require('selenium-webdriver');
let BasePage = require('../pages/base-page.js');
const WAITING_ELEMENT_TIMEOUT = 5000 //milis

class LoginPage extends BasePage {
    
    constructor(){
        super()
        this.usernameField = By.id("email");
        this.passwordField = By.id("password");
        this.loginBtn = By.xpath("//button[@type='submit']");
        this.loginFailedWarning = By.xpath("//div[contains(@class, 'message-error')]");

        this.warningFailedLoginWrongEmailPassword = "Email atau Password salah."
    }

    async goToPage(){
        await this.goToUrl("https://www.monotaro.id/customer/account/login/");
    }

    async webElementFactory(locator){
        return this.driver.findElement(locator);
    }

    async inputUsernameField(userName) {
        this.webElementUsernameField = this.driver.findElement(this.usernameField);
        await this.driver.wait(until.elementIsVisible(this.webElementUsernameField), WAITING_ELEMENT_TIMEOUT);
        await this.webElementUsernameField.sendKeys(userName);
    }

    async inputPasswordField(password) {
        this.webElementPasswordField = this.driver.findElement(this.passwordField);
        await this.driver.wait(until.elementIsVisible(this.webElementPasswordField), WAITING_ELEMENT_TIMEOUT);
        await this.webElementPasswordField.sendKeys(password);
    }

    async clickSubmitBtn(){
        this.webElementSubmitBtn = this.driver.findElement(this.loginBtn);
        await this.driver.wait(until.elementIsVisible(this.webElementSubmitBtn), WAITING_ELEMENT_TIMEOUT);
        await this.webElementSubmitBtn.click();
    }
};

module.exports = new LoginPage();

test/login-test.js

const loginPage = require('../pages/login-page.js');
const expect = require('chai').expect;
const assert = require('chai').assert;


describe('Login Page Test', function() {
    beforeEach(async function() {

    });

    afterEach(async function() {
        await loginPage.closeBrowser()
        sleep(3)
    });

    it('LO_2 - (+) Open Login page', async function() {
        await loginPage.goToPage();
    });

    it('LO_3 - (+) Login with valid email and valid password', async function() {
        await loginPage.goToPage();
        await loginPage.inputUsernameField("monotaroid@yopmail.com");
        await loginPage.inputPasswordField("Monotaroid1!");
        await loginPage.clickSubmitBtn()
    });

    function sleep(second) {
        return new Promise(resolve => setTimeout(resolve, second*1000));
    }
});

and when I run the test, 1st test will always run smoothly.

But when it comes to x test / the rest of case, it keeps telling me NoSuchSessionError: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.

I also tried to modify the code and I have got this: NoSuchSessionError: invalid session id

Yes, the session is terminated afterEachTest. But why it does not automatically create a new one? Can someone guide me through this, please?

Thanks!

ps: I already try to look this but it does not bring a solution

pss: I also already tried to use driver.reloadSession() but, it returns ReferenceError: driver is not defined

yuri
  • 3
  • 4

0 Answers0