硒黄瓜无法检查图像

问题描述 投票:0回答:1

我试图使用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));
            })
    },

所以我的问题是,我无法检测到图像,因此测试失败了。如何解决这个问题?

selenium-chromedriver gherkin cucumberjs
1个回答
0
投票

我只是通过检查看不到问题,但你可以尝试一些事情:

  • 使用FindElement而不是FindElements。如果没有找到该元素,FindElement将(有帮助)立即失败,而不是愉快地找不到任何东西并且在下游传递一个空数组以更加混乱的方式失​​败。
  • 我有一些问题,当断言运行时,页面根本没有处于预期的状态,这在几毫秒后查看页面时非常令人沮丧,而且就在那里。您可以尝试在断言之前将整个DOM转储到控制台(我认为它是driver.getPageSource(),但不要引用我)。

你没有要求的一些无偿的,厚颜无耻的代码审查(对不起):

  • 当我调试时,我发现步骤定义遵循与特征文件相同的流程非常有用,并且确保每个步骤在两个地方都具有相同的Given / When / Then前缀(这是一种尼特,但你有一个当时的当时)。
  • 如果你让正则表达式尽可能地缩小,有时会出现一系列问题。我怀疑这与您的直接问题有关,但是这些位置编号捕获组可以定义为仅接受数字(%d+),然后如果您愿意,甚至可以删除双引号。
  • 我很幸运在页面对象的单个位置定义了定位器(例如by.css('.cardImage')),所以它们没有重复
© www.soinside.com 2019 - 2024. All rights reserved.