无法在 Protractor Cucumber Framework 中的 webElements 上单击或发送键

unable to click or sendkeys on webElements in Protractor Cucumber Framework

我的代码工作正常,没有任何故障,但没有执行任何点击或发送键或任何其他操作。浏览器自动关闭,它甚至没有响应 browser.sleep 命令。当使用 console.log 它正在打印文本,但元素操作不起作用

StepDef 文件

var page=require("..\src\StepDefFiles\testpage.js");
var test=function(){
     Given('Load the URL', function () {
        page.browserInit();
       });
     
       Given('Get the Title', function () {
           // Write code here that turns the phrase above into concrete actions
          browser.getTitle().then(function(title){
              console.log(title);
          })
         });
       
       Then('Login in to the account', function () {
             page.gmailLink();
         
         });
       

       Then('validate the home page', function () {
          browser.getTitle().then(function(Title){
              if(Title.indexOf("sign in")!=-1){
                  console.log(Title);
              }
          })
       });
}
module.exports=new test();

测试页面文件

var testPage=function(){
    
    this.browserInit=function(){
         browser.ignoreSynchronization=true;
         browser.get("https://google.com");
         browser.sleep(5000);
         browser.manage().window().maximize();
         browser.sleep(5000);
    }
    
    this.gmailLink=function(){
        element(By.xpath("//a[text()='Gmail']")).click();
    }
    
}
module.exports=new testPage();

配置文件

exports.config = {

seleniumAddress: 'http://localhost:4444/wd/hub',
getPageTimeout: 60000,
allScriptsTimeout: 500000,
framework: 'custom',
// path relative to the current config file
frameworkPath: require.resolve('C:\Users\DELL\node_modules\protractor-cucumber-framework'),
capabilities: {
    'browserName': 'chrome'
},

// Spec patterns are relative to this directory.
specs: [
    '..\Protractor_Cucumber\src\FeatureFiles\Test.feature'
],

cucumberOpts: {
    require: '..\Protractor_Cucumber\src\StepDefFiles\stepDef.js',
    tags: false,
    profile: false,
    'no-source': true
},
 onPrepare: function () {
const {Given, Then, When, Before} = require('C:\Users\DELL\node_modules\cucumber');
global.Given = Given;
global.When = When;
global.Then = Then;
global.Before = Before;
  }
};

专题文件

Feature: Title of your feature
  I want to use this template for my feature file

  Scenario: Title of your scenario
    Given Load the URL
    And   Get the Title
    Then  Login in to the account
    And   validate the home page

控制台日志

21:41:37] I/launcher - Running 1 instances of WebDriver
[21:41:37] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
.....

1 scenario (1 passed)
4 steps (4 passed)
0m00.030s
Google
[21:41:46] I/launcher - 0 instance(s) of WebDriver still running
[21:41:46] I/launcher - chrome #01 passed

如果有人能回答这个问题,那将对我有很大帮助

  1. 无需将step函数包装到另一个函数中导​​出,详情见here
  2. 您需要 return 对每个步骤功能的承诺,详情请见 here

如下更改您的 StepDef 文件:

var page = require("../src/StepDefFiles/testpage.js");

Given('Load the URL', function() {
  return page.browserInit();
});

Given('Get the Title', function() {
  // Write code here that turns the phrase above into concrete actions
  return browser.getTitle().then(function(title) {
    console.log(title);
  })
});

Then('Login in to the account', function() {
  return page.gmailLink();
});

Then('validate the home page', function() {
  return browser.getTitle().then(function(Title) {
    if (Title.indexOf("sign in") != -1) {
      console.log(Title);
    }
  })
});