4

This question might be a duplicate, but i am not as expected, so raising again.

I am creating a new protractor framework in our project. My application has login screen which i need to login with different user details for each case in a feature. I have two scenarios in a feature file. When i run, the browser should open the login page and do some action and close the browser for each scenario, and it has to do the same thing for every scenario, but am not seeing this happen. When i run, it happens for first scenario and from second it fails. Could someone help me on this?

i have tried with different options with After hook, restartBrowserBetweenTests but no luck.

conf.js

import { browser } from "protractor";

export const config = {
    // chromeDriver: './drivers/chromedriver',
    // seleniumServerJar: './drivers/selenium-server-standalone-3.12.0.jar',
    seleniumAddress: 'http://localhost:4444/wd/hub',
    // baseUrl: '<url>',
    SELENIUM_PROMISE_MANAGER: false,
    framework: 'custom',
    frameworkPath: require.resolve('protractor-cucumber-framework'),
    capabilities: {
        'browserName': 'chrome',
        'chromeOptions': {
            'args': ['--disable-extensions=true']
        }
    },
    // restartBrowserBetweenTests: true,
    specs: [
        './features/*.feature'
    ],
    cucumberOpts: {
        require: [
            './features/step_definitions/*.js',
            './support/*.js'
        ],
        format: ['json:results/report.json'],
        tags: [
            //"@smoke",
            //"@regression"
        ],
        strict: true
    },
    disableChecks: true,
    onPrepare: function () {
        browser.manage().deleteAllCookies();
        browser.manage().window().maximize();
        browser.ignoreSynchronization = true;
    },
    getPageTimeout: 100000,
    allScriptsTimeout: 500000
}

hooks.ks

import { Before, BeforeAll, After, AfterAll } from "cucumber";
import { browser } from "protractor";
import { config } from "../protractorConf"

Before( {timeout: 2 * 20000}, async () => {
    // await browser.get(config.baseUrl)
})

// After( {timeout: 2 * 5000}, async () => {
//     await browser.close()
// })

AfterAll( {timeout: 2 * 5000}, async () => {
    await browser.quit()
})

steps.js

import { Given, When, Then } from "cucumber"
import { expect } from "chai";
import { browser } from "protractor";

import * as loginPage from "../pages/loginPage"
import * as welcomePage from "../pages/welcomePage"
import * as verificationPage from "../pages/verificationPanelPage"
import * as homePage from "../pages/homePage";

const stepTimeoutExpiry = {timeout: 2 * 5000}
let globalLoanNumber = ""

Given(/^Launch SATURN app with "(.*?)" and "(.*?)"$/, stepTimeoutExpiry, async (user, pass) => {
    await browser.get('<url>')
    await loginPage.login(user, pass)
})

When(/^I search a loan by "(.*?)" as "(.*?)" in search page$/, stepTimeoutExpiry, async (searchType, loanNumber) => {
    globalLoanNumber = loanNumber
    await welcomePage.selectSearchType()
    if (searchType === "Loan Number") {
        await welcomePage.selectLoanNumber()
    }
    await welcomePage.enterLoanNumber(loanNumber)
    await welcomePage.clickSearchAccountBtn()
})


When(/^I skip the verification details on verification page$/, stepTimeoutExpiry, async () => {
    await verificationPage.skipVerification()
})

Then(/^I must see the Saturn "(.*?)" page for this account$/, stepTimeoutExpiry, async (homeText) => {
    // await homePage.isHomeLinkAvailable()
    expect(await homePage.isHomeLinkAvailable()).to.be.true
    expect(await homePage.getAccountNumberText()).to.equal(globalLoanNumber)
})

feature file

Feature: Running sample feature

    Scenario: Login to Saturn and verify it works
    Given Launch SATURN app with "user" and "pass"
    When I search a loan by "Loan Number" as "0535003974" in search page
    And I skip the verification details on verification page
    Then I must see the Saturn "Home" page for this account

    Scenario: Login to Saturn and verify it works for the second time
    Given Launch SATURN app with "user" and "pass"
    When I search a loan by "Loan Number" as "0535003974" in search page
    And I skip the verification details on verification page
    Then I must see the Saturn "Home" page for this account

below is the error, when i run.

λ npm run test

> protractortest@1.0.0 test c:\<user>\ATDDProtractorTest
> babel-node node_modules/protractor/bin/protractor protractorConf.js --presets-env

(node:12828) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
[10:55:12] I/launcher - Running 1 instances of WebDriver
[10:55:12] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
.......F---.

Failures:

1) Scenario: Login to Saturn and verify it works for the second time # features\testone.feature:9
   √ Before # support\hooks.js:5
   × Given Launch SATURN app with "user" and "pass" # features\step_definitions\testOne.steps.js:13
       Error: function timed out, ensure the promise resolves within 10000 milliseconds
           at Timeout._onTimeout (c:\Manimaran\ATDDProtractorTest\node_modules\cucumber\src\user_code_runner.js:61:18)
           at ontimeout (timers.js:475:11)
           at tryOnTimeout (timers.js:310:5)
           at Timer.listOnTimeout (timers.js:270:5)
   - When I search a loan by "Loan Number" as "0535003974" in search page # features\step_definitions\testOne.steps.js:19
   - And I skip the verification details on verification page # features\step_definitions\testOne.steps.js:30
   - Then I must see the Saturn "Home" page for this account # features\step_definitions\testOne.steps.js:34
   √ After # node_modules\protractor-cucumber-framework\lib\resultsCapturer.js:25

2 scenarios (1 failed, 1 passed)
8 steps (1 failed, 3 skipped, 4 passed)
0m20.442s
[10:55:40] I/launcher - 0 instance(s) of WebDriver still running
[10:55:40] I/launcher - chrome #01 failed 1 test(s)
[10:55:40] I/launcher - overall: 1 failed spec(s)

loginpage.js

import { element, by, browser } from "protractor";

const userName = element(by.id('username'));
const passWord = element(by.id('password'));
const signOn = element(by.xpath('.//a[contains(@title,"Sign In")]'));

const EC = protractor.ExpectedConditions
const isVisibilityOf = EC.visibilityOf(userName)

export const login = async (user, pass) => {
    await browser.wait(isVisibilityOf, 10000)
    await userName.sendKeys(user);
    await passWord.sendKeys(pass);
    await signOn.click();
}
mmar
  • 1,840
  • 6
  • 28
  • 41
  • any help on this please? – mmar May 18 '18 at 19:05
  • In those hooks, you'll want to do a browser.quit() in the after and spawn a new browser in the before. Unfortunately I don't use protractor in my tests, or this would be an answer, but I currently create a new driver and quit the driver this way and that works, so if you can translate that over to protractor, you should be good – KyleFairns May 18 '18 at 23:08
  • @KyleFairns, I have tried as you suggested, but its not. BTW, i do not have logout feature in my application. I am unable to open new browser window after each scenario. It just continues in same browser. Due to SSO login, I have to restart browser after each scenario – mmar May 21 '18 at 20:47

1 Answers1

0

For protractor, before any spec be executed, an browser instance(session) will be init and hand over the session to spec runner which manage to execute specs.

If you want to close session after each scenario/feature, you have to create an new session and pass down the new session to the original spec runner. I think it's not easy to implement this for us without learning protractor source code deeply.

I prefer to recommend following solutions more easy from our side:

1) delete cookie in After hook, protractor supply such API to do that

2) open logout url in After hook if your app has such url, otherwise you can click logout button.

Actually, you can put the delete cookie, open logout url or click logout button at the beginning of your login function, then open login url. With that After hook is unnecessary.

yong
  • 13,357
  • 1
  • 16
  • 27
  • I have tried as you suggested, but its not. BTW, i do not have logout feature in my application. I am unable to open new browser window after each scenario. It just continues in same browser. Due to SSO login, I have to restart browser after each scenario – mmar May 21 '18 at 20:48