javascript - 无法添加新的给定/何时/然后,出现错误 `SyntaxError: Invalid regular expression: missing/`

标签 javascript protractor gherkin cucumberjs

我已经配置了 cucumber + Protractor ,我首先将stepDefinitions分成不同的文件,如下所示:

stepdefinitions

当我创建新功能文件并开始运行时, cucumber /Protractor 无法识别我添加到其他文件中的这些新步骤。因此,我决定将所有新步骤移至同一个文件中。

但是当我运行时,尽管它们写得很好(检查和比较了数千次),但我收到此错误:

    [launcher] Error: /Users/brunosoko/Documents/Dev/Personal/test2/features/step_definitions/homepage/homepage.js:30
    this.When(/^I select an image
     or video$/, function (done) {
              ^
SyntaxError: Invalid regular expression: missing /
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at /Users/brunosoko/Documents/Dev/Personal/test2/node_modules/cucumber/lib/cucumber/cli/support_code_loader.js:63:29
    at Array.forEach (native)
    at Object.wrapper (/Users/brunosoko/Documents/Dev/Personal/olapic-test2/node_modules/cucumber/lib/cucumber/cli/support_code_loader.js:62:15)

我正在运行以下版本的 Protractor 和 cucumber :

  "devDependencies": {
    "chai": "*",
    "chai-as-promised": "^5.1.0",
    "cucumber": "~0.6.0",
    "protractor": "1.4.0",
    "protractor-cucumber-junit": "latest",
    "protractor-html-screenshot-reporter": "^0.0.21",
    "selenium-webdriver": "2.47.0"   },

这些是我的步骤定义:

```
    /*Given*/
    this.Given(/^I am at the homepage$/, function (done) {
        browser.get('').then(function(){
            CarouselPage.clickOutSidePopUp();
            done();
        });
    });
    this.Given(/^I can see that images and videos are present on the widget$/ 
    , function (done) {
        expect(CarouselPage.checkMediaSource()).to.eventually.be.true;
        done();
    });


    this.When(/^I select an image
     or video$/, function (done) {
        CarouselPage.getMedia(1).click();
        done();
    });


    /*Then*/
    this.Then(/^I see that Carousel Widget is correctly displayed$/, function(done){
        expect(CarouselPage.carouselContainer.isPresent()).to.eventually.be.true;
        done();
    });

    this.Then(/^I see the viewer modal
    
    
    $/, function(done){
        expect(ViewerModal.viewerContainer.isPresent()).to.eventually.be.true;
        done();
    });

    this.Then(/^I see the Gallery widget
    $/, function(done){
        expect(CarouselPage.carouselContainer.isPresent()).to.eventually.be.true;
        done();
    }); ```

那么,我做错了什么?我需要清除一些缓存吗?这是我正在使用的版本的错误吗?

谢谢!

注意: 还引起我注意的是,如果我评论整个步骤,我会在控制台上看到这个

./node_modules/protractor/bin/protractor conf.js
util.puts: Use console.log instead
Starting selenium standalone server...
util.puts: Use console.log instead
Selenium standalone server started at http://192.168.0.101:58696/wd/hub
[launcher] Error: /Users/brunosoko/Documents/Dev/Personal/test2/features/step_definitions/homepage/stepsDefinitions.js:24
    //this.When(/^I select an image
     or video$/, function (done) {
                                        ^^^^^^
SyntaxError: Unexpected identifier
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at /Users/brunosoko/Documents/Dev/Personal/test2/node_modules/cucumber/lib/cucumber/cli/support_code_loader.js:63:29
    at Array.forEach (native)
    at Object.wrapper (/Users/brunosoko/Documents/Dev/Personal/test2/node_modules/cucumber/lib/cucumber/cli/support_code_loader.js:62:15)

编辑2: 这是我的 conf.js 文件,以防您发现我在这里也做错了什么

exports.config = {
    specs: [
        'features/*.feature'
    ],
    baseUrl: "http://www.page.com",
    multiCapabilities: [
        {
            'browserName': 'chrome'
        }
    ],
    framework: 'cucumber',
    //seleniumAddress: 'http://localhost:4444/wd/hub',
    cucumberOpts: {
        require: 'features/step_definitions/**/*.js',
        format: 'pretty'
    },
    resultJsonOutputFile: 'report.json',

    onPrepare: function () {

        browser.driver.manage().window().maximize();

        browser.ignoreSynchronization = true;

        browser.manage().timeouts().implicitlyWait(20000);

        browser.getCapabilities().then(function (cap) {
            browserName = cap.caps_.browserName;
        });

    }
};

最佳答案

image 之后的第一个字符是 unicode line separator 。删除它,节点将能够解析正则表达式。

关于javascript - 无法添加新的给定/何时/然后,出现错误 `SyntaxError: Invalid regular expression: missing/`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33389270/

相关文章:

javascript - JSF TextArea 的最大长度

javascript - wkhtmltoimage - 有时获取整个页面,有时不获取

java - Selenium 网络驱动程序 : Automatically Repeating a successful scenario

cucumber - 如何创建 cucumber 数据表?

javascript - signalR 核心定义传输

javascript - 调用window.setInterval会在chrome中打开一个新线程吗?

angularjs - 如何使用 Protractor 捕获 AngularJS 错误

javascript - Protractor - 识别 Ui-Sref 链接

javascript - 失败 : invalid element state on attempt to . sendKeys() 到文本字段

java - Cucumber Java with Selenium - 重用登录功能并将其与其他功能连接起来