当前位置: 首页 > 知识库问答 >
问题:

如何在功能-量角器和cucumberjs中为每个场景关闭和打开浏览器

卓正业
2023-03-14

这个问题可能是重复的,但我不像预期的那样,所以再次提出。

我正在我们的项目中创建新的量角器框架。我的应用程序有登录屏幕,我需要在一个功能的每个案例中使用不同的用户详细信息登录。我在一个特征文件中有两个场景。当我运行时,浏览器应该打开登录页面,执行一些操作,并为每个场景关闭浏览器,它必须为每个场景做同样的事情,但我没有看到这种情况发生。当我运行时,它发生在第一个场景中,从第二个场景开始它就失败了。有人能帮我吗?

我已经尝试了不同的选项后钩子,重新启动BrowserBetween测试,但没有运气。

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()
})

步骤.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: 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

下面是我运行时的错误。

λ 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)

登录页面.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();
}

共有1个答案

仲鸿风
2023-03-14

对于量角器,在执行任何规范之前,浏览器实例(会话)将被初始化,并将会话移交给规范运行者,后者负责执行规范。

如果您想在每个场景/功能之后关闭会话,您必须创建一个新会话并将新会话传递给原始规范运行器。我认为在没有深入学习量角器源代码的情况下,对我们来说实现这一点并不容易。

我倾向于推荐以下我们认为更简单的解决方案:

1)在钩子之后删除cookie,量角器提供这样的API来做到这一点

2) 如果您的应用有这样的 url,请在钩子后打开注销 URL,否则您可以单击注销按钮。

实际上,您可以在登录功能的开头放置删除cookie,打开注销URL或单击注销按钮,然后打开登录URL。有了那个 钩子 是不必要的。

 类似资料:
  • 我在我的UI自动化框架中使用QAF开源Java库,并希望在每个测试中打开和关闭浏览器。但是,不能用下面的代码完成,因此由testSuccessfulLogin()打开的浏览器保持打开状态,因此testFailedLogin()失败。

  • 我有这个功能文件 方案概要: 给定用户在我的网站上 当用户是管理员或主控编辑器时 3.用户在 /page/页面 4.和用户单击/标题/链接 5.然后用户应该被带到/目标页面/ 对于“给定用户在我的网站上”,我编写了登录详细信息,这将为场景中的每个示例打开新的浏览器。我希望单一浏览器打开并执行所有示例。有人能帮忙吗

  • 问题内容: 我希望使用Protractor,CucumberJS和Jasmine来测试我的项目。如何在量角器中同时使用Jasmine和CucumberJS?这是我创建的项目设置: / path / to / myproj / protractor.conf.js 如您所见,该项目使用“黄瓜”作为框架。如何与CucumberJS一起添加Jasmine框架?是通过量角器配置文件还是代码中的其他位置?

  • 我的控制台: RG.OpenQA.Selenium.Remote.UnreachableBrowserException:无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败。生成信息:版本:“3.4.0”,修订版:“Unknown”,时间:“Unknown”系统信息:主机:“WS00MU016”,IP:“”,OS.Name:“Windows 10”,OS.ARCH:“AMD64”,OS

  • 本文向大家介绍在Nginx浏览器中打开目录浏览功能,包括了在Nginx浏览器中打开目录浏览功能的使用技巧和注意事项,需要的朋友参考一下 在nginx中不像apache默认是打开目录浏览功能的,在nignx中目录浏览功能默认是关闭了,下面我来介绍在nginx中实现目录浏览功能的配置方法。 打开nginx.conf文件,在location server 或 http段中加入 另外两个参数最好也加上去: