我试图使用selenium-cucumber-js
测试我的一个Web应用程序。
我有一个使用Gherkin语法编写的Feature文件。
Feature: UPS XXX Troubleshooting
#
##Chatbot must start automatically
Scenario: Chatbot automatically says hello
Given a chat window "localhost:3000/"
Then the text displayed in position "1" shall contain following statements
| statement |
| Hey there! I’m a bot that can help you troubleshoot issues with UPS. |
| We’ll first need to determine exactly which UPS you have by collecting the model and serial number from its bottom or rear panel. It will be found on a white, rectangular sticker with two barcodes. |
| Please enter your Model Number from the barcode sticker |
And there shall be at least an embedded image in position "2" containing "UPSBarCode.jpg" file
我在下面写了步骤定义
const timeout=60000
module.exports = function () {
this.Given(/^a chat window "([^"]*)"$/, function (url) {
return helpers.loadPage(url);
})
this.When(/^there shall be at least an embedded image in position "([^"]*)" containing "([^"]*)" file$/, function (position, jpgOrPng) {
return page.apcupsTroubleshooting.expectImage(jpgOrPng, position, timeout);
})
this.Then(/the text displayed in position "([^"]*)" shall contain following statements$/, function (position, table) {
table.rows().forEach(function (option, index) {
console.log("checking " + option);
return page.apcupsTroubleshooting.expectChatbotText(option[0], position, timeout);
});
})
};
这是我的Page对象
const expect = require('chai').expect;
module.exports = {
url: 'localhost:3000/',
elements: {
textInput: by.name("inputText"),
textOutputs: by.className("message")
},
/**
* types something in chatbot
* @param {any} userInput
*/
typeMessage: function (userInput) {
console.log("Going to type '" + userInput + "'");
var selector = page.apcupsTroubleshooting.elements.textInput;
return driver.findElement(selector).sendKeys(userInput, selenium.Key.ENTER);
},
expectChatbotText: function (text, position, timeout) {
console.log("checking '" + text + "'");
theXpath = "//div[@class='message']//p[contains(text(),\"" + text + "\")]";
console.log("xpath:" + theXpath)
return driver.wait(until.elementsLocated(by.xpath(theXpath)), timeout)
.then(() => {
driver.findElement(by.xpath(theXpath)).getText()
.then(t => {
try {
expect(t).to.contain(text)
}
catch (e)
{
return Promise.reject(false)
}
})
})
},
expectImage: function (imageFile, position, timeout) {
console.log("checking image " + imageFile + " existance in position " + position);
theXpath = " //img[contains(@src,'" + imageFile + "')]";
return driver.wait(
until.elementsLocated(by.css('.cardImage')), timeout)
.then(() => {
console.log('xxxxxxxxxxxxxxxxxxxx')
driver.FindElements(by.css('.cardImage'))
.then(t => {
console.log('yyyyyyyyyyyyyy')
try {
expect(t[0].GetAttribute("src").ToString()).to.contain(imageFile)
}
catch (e)
{
return Promise.reject(false)
}
})
})
},
checkOptionElement: function (option, position, timeout) {
console.log("checking list entry " + option + " existance in frame " + position);
theXpath = " //div[@class='card']/div/ul/li[contains(text(),\"" + option +"\")]";
return driver.wait(until.elementsLocated(by.xpath(theXpath)), timeout)
.then(() => {
return driver.findElement(by.xpath(theXpath));
})
}
};
这是我的html页面
<section class="messages-wrapper">
<div class="messages">
<div class="group group-bot" id="message-group-bot-1">
<div>
<div>
<div class="messageParentContainer">
<div class="message">
<p>
<div>Hey there! I’m a bot that can help you troubleshoot issues with UPS.</div>
</p>
</div>
</div>
</div>
</div>
<div>
<div>
<div class="">
<div class="message">
<p>
<div>We’ll first need to determine exactly which UPS you have by collecting the model and serial number from its bottom or rear panel. It will be found on a white, rectangular sticker with two barcodes.</div>
</p>
<div class="cardImageContainer"> <img class="cardImage" src="https://somedomain/UPSBarCode.jpg"></div>
<div class="hide-container"> <object class="videoFrame" data=""></object></div>
</div>
</div>
</div>
</div>
<div>
<div>
<div class="messageParentContainer">
<div class="message">
<p>
<div>Please enter your Model Number from the barcode sticker</div>
</p>
</div>
</div>
</div>
</div>
</div>
<div style="float: left; clear: both;"></div>
</div>
</section>
我也试过了
expectImage: function (imageFile, position, timeout) {
console.log("checking image " + imageFile + " existance in position " + position);
theXpath = " //img[contains(@src,'" + imageFile + "')]";
return driver.wait(until.elementsLocated(by.xpath(theXpath)), timeout)
.then(() => {
return driver.findElement(by.xpath(theXpath));
})
},
所以我的问题是,我无法检测到图像,因此测试失败了。如何解决这个问题?
我只是通过检查看不到问题,但你可以尝试一些事情:
FindElement
而不是FindElements
。如果没有找到该元素,FindElement
将(有帮助)立即失败,而不是愉快地找不到任何东西并且在下游传递一个空数组以更加混乱的方式失败。driver.getPageSource()
,但不要引用我)。你没有要求的一些无偿的,厚颜无耻的代码审查(对不起):
%d+
),然后如果您愿意,甚至可以删除双引号。by.css('.cardImage')
),所以它们没有重复