简体   繁体   English

TypeError:无法读取未定义的属性(读取“v”)

[英]TypeError: Cannot read properties of undefined (reading 'v')

I am trying to run a scenario by grouping my repetitive steps in cucumber background.我正在尝试通过在黄瓜背景中对重复步骤进行分组来运行一个场景。 I am using an excel sheet to give username and password as i don't want to hardcode it in feature file.我正在使用 Excel 表来提供用户名和密码,因为我不想在功能文件中对其进行硬编码。

but while executing its hitting "TypeError: Cannot read properties of undefined (reading 'v')"但是在执行其命中“TypeError:无法读取未定义的属性(读取'v')”时

However if I remove the background and put everything in the scenario and run then it works fine.但是,如果我删除背景并将所有内容放入场景中并运行,那么它可以正常工作。

Can anybody help me what I am missing here.任何人都可以帮助我在这里缺少什么。

Feature file特征文件

Feature:  UI Testing

  Background: User log-in
  Given I am on the login page
  When I enter <email>
  When I enter <password>
  When  I click on sign in button
  Then  I should see home page

  Scenario: Create template 1
    When I Click on the templates-1
    When I fill in the required details and <desc> click create
    Then I validate template is created successfully
    Then I should click sign out button
    Examples:
      |email|password|desc|
      |A3|B2|testing|

  Scenario: Create template 2
    When I Click on the templates-2
    When I fill in the required details and <desc> click create
    Then I validate template is created successfully
    Then I should click sign out button
    Examples:
      |email|password|desc|
      |A3|B2|testing|

(A3 and B2 are cells of excel sheet) (A3和B2是excel表格的单元格)

Step Def步骤定义

Given(/^I am on the login home page$/, function () {
    loginPage.openPage()
});
When(/^I enter email (.+)$/, async(email) => {
    var excelvalue = common.readExcelData(email);
    await mazeHome.username(excelvalue)
});
When(/^I enter password (.+)$/, async(password) => {
    var excelvalue = common.readExcelData(password);
    await mazeHome.password(excelvalue)
});

and here my common function where i wrote the logic of feeding data from excel.这里是我的常用函数,我在其中编写了从 excel 提供数据的逻辑。

const { default: ChromeDriverService } = require("wdio-chromedriver-service");
var XLSX = require('xlsx');

class commonFunctions  {

     readExcelData(cellnumber) {

        var workbook = XLSX.readFile('./testData.xlsx');
        var worksheet = workbook.Sheets['Sheet1'];
        var celldata = worksheet[cellnumber].v;
        return celldata;
    }

You should check the value in these functions.您应该检查这些函数中的值。 It should take valid values like 'A3' for email and 'B2' for password它应该采用有效值,例如电子邮件的“A3”和密码的“B2”

 When(/^I enter email (.+)$/, async(email) => { console.log(1,email); var excelvalue = common.readExcelData(email); await mazeHome.username(excelvalue) }); When(/^I enter password (.+)$/, async(password) => { console.log(2,password); var excelvalue = common.readExcelData(password); await mazeHome.password(excelvalue) });
And you should handle not found call on readexcel function 你应该处理 readexcel 函数上的 not found 调用

 readExcelData(cellnumber) { var workbook = XLSX.readFile('./testData.xlsx'); var worksheet = workbook.Sheets['Sheet1']; if(!worksheet[cellnumber]){ // handle not found cell return null; } var celldata = worksheet[cellnumber].v; return celldata; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义的属性(读取“createdTimestamp”) - TypeError: Cannot read properties of undefined (reading 'createdTimestamp') TypeError:无法读取未定义的属性(读取“forEach”) - TypeError: Cannot read properties of undefined (reading 'forEach') TypeError:无法读取未定义的属性(读取“学生”) - TypeError: Cannot read properties of undefined (reading 'students') TypeError:无法读取未定义的属性(读取“addEventListener”) - TypeError: Cannot read properties of undefined (reading 'addEventListener') TypeError:无法读取未定义的属性(读取&#39;indexOf&#39;) - TypeError: Cannot read properties of undefined (reading 'indexOf') TypeError:无法读取未定义的属性(读取“国家”) - TypeError: Cannot read properties of undefined (reading 'country') TypeError:无法读取未定义的属性(读取“然后”) - TypeError: Cannot read properties of undefined (reading 'then') TypeError:无法读取未定义的属性(读取“设置”) - TypeError: Cannot read properties of undefined (reading 'set') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') TypeError:无法读取未定义的属性(读取“问题”) - TypeError: Cannot read properties of undefined (reading 'ques')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM