Я пытался протестировать одно из моих веб-приложений, используя selenium-cucumber-js
.
У меня есть файл Feature, написанный с использованием синтаксиса Gherkin.
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
Я написал определения Stepниже
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));
})
},
Так что моя проблема в том, что я не могу обнаружить изображение, так что тесты не пройдены.Как решить эту проблему?