获取第一个 <p> 标签的文本并在 Mocha 和 Selenium 的自动化测试脚本中检查是否成功登录

Getting the text of first <p> tag and check for successful login in an automation test script in Mocha and Selenium

我有一个自动测试脚本,我正在尝试 运行 在 selenium 环境中使用 mocha

脚本首先登录,然后检查所有 <p> 标签的文本。

如果任何 <p> 标签的文本与短语“”欢迎来到管理页面!“”匹配,则登录尝试将被视为成功。否则就是失败的尝试。

我想更改密码。无需遍历所有

标记,我只需要检查第一个

标记。我该怎么做?

我想记录细节,尝试是成功还是失败。

我的代码如下。

var assert = require('assert');
var test = require('selenium-webdriver/testing');
var webdriver = require('selenium-webdriver');
var By = webdriver.By;
var until = webdriver.until;
var equals = webdriver.equals;

test.describe('TrackRevenue Test', function() 
{
  test.it('should work', function() 
  {
        var driver = new webdriver.Builder()
                    .withCapabilities(webdriver.Capabilities.phantomjs())
                    .build();
        var loginFlag = 0;
        var baseUrl = 'http://saswatr3.ouh.co/login';
        var expectedTitle = "Track Revenue";
        var successMessage = "Welcome to the admin page!";
        driver.get(baseUrl);
        driver.getTitle().then(function(title) 
        {
            if(expectedTitle === title)
            {
                console.log("Verification Successful - The correct title is displayed on the web page.");
            }
            else
            {
                console.log("Verification Failed - An incorrect title is displayed on the web page.");
            }
        });
        driver.findElement(By.id('username')).sendKeys('saswat@matrixnmedia.com');
        driver.findElement(By.id('password')).sendKeys('DarkPrince2012');
        driver.findElement(By.id('_submit')).click();
        driver.wait(until.titleIs('Track Revenue'), 1000);
        driver.findElements(By.tagName('p')).then(function (pTag) 
        {
            pTag.forEach(function(p) //I am iterating through all the <p>. I need to get only the first <p>
            {
                p.getText().then(function(text) 
                {
                    if(text.toLowerCase() === successMessage.toLowerCase())
                    {
                        loginFlag = 1;
                    }
                    //done(); //mocha async callback
                }); 
            });
            if(loginFlag ==1)
                console.log("Login Successful");
            else
                console.log("Login Unsuccessful");
        });
    driver.quit();
  });
});

<p> 部分的 html 部分有“欢迎来到管理页面!”

<div class="wrapper wrapper-content animated fadeIn">
   <div class="row">
     <div class="col-centered col-xs-12">
     <div class="ibox float-e-margins">
     <div class="ibox-title">
       <h5>Links</h5>
       <div class="ibox-tools"> </div>
     </div>
     <div class="ibox-content">
        <p>Welcome to the admin page!</p>
        <p>Please use the menu on the left</p>
     </div>
     </div>
     </div>
   </div>
</div>

PS.

我需要对代码进行更多修改。在任何情况下,如果登录失败,登录页面都会显示 div 文本:“Invalid credentials.

段的html是这样的:

<div class="text-danger text-danger-orange">Invalid credentials.</div>

我也想要一个检测此部分的检查脚本。

编辑:

当我放这段脚本时,我的脚本运行没问题:

driver.findElements(By.xpath("//p[contains(text(), 'Welcome to the admin page!')]")).then(function()
        {
            loginFlag = 1;
            if(loginFlag ==1)
                console.log("Login Successful");
            else
                console.log("Login Unsuccessful");
        });

但是当我输入这段脚本时,我得到了一个 类型错误:未定义不是函数

if(driver.findElements(By.xpath("//p[contains(text(), 'Welcome to the admin page!')]")).size() != 0)
{
   loginFlag = 1;
}
else
{
    //"Welcome to the admin page" not found
    if(driver.findElements(By.xpath("//div[contains(text(), 'Invalid Credentials!')]")).size() != 0)
    {
        //do what you have to do if "Invalid credentials" is found
    }
    else
    {
        // "Invalid credentials not found"
    }
}

使用 XPATH 可以让您的生活更轻松。例如:

if(driver.findElements(By.xpath("//* [text()[contains(.,'Welcome to the admin page!')]]")).size() != 0){
            //do what you have to do if "Welcome to the admin page" is found
        }else{
            //"Welcome to the admin page" not found
            if(driver.findElements(By.xpath("//* [text()[contains(.,'Invalid credentials')]]")).size() != 0){
                //do what you have to do if "Invalid credentials" is found
            }else{
                // "Invalid credentials not found"
            }
        }

希望对您有所帮助。

编辑:

如您所愿,详细说明:

使用 xpath,您可以简单地搜索页面上应该出现的所需文本。如果您只想查找字符串 "Welcome to the admin page""Invalid credentials" 而不管页面上的位置,您可以轻松地按以下方式搜索文本:

//* [text()[contains(.,'<your string text here>')]]

它检查节点的 getText() 方法是否包含给定的文本。

如果要检查整个页面是否有结果,可以使用 findElements,因为它将 return all 找到 WebElements。如果在给定的 xpath 中找不到任何东西,它 return 一个空的 "array",大小为 0。所以如果你使用

driver.findElements(By.xpath("//* [text()[contains(.,'Welcome to the admin page!')]]")).size() != 0

您可以检查该方法是否已找到包含所需文本的元素。

就这些,其他的就简单了if/else..你可以随意使用上面提到的xpath和函数。因此,与其遍历所有 <p> 标签,不如简单地使用上面提到的 xpath。

EDIT2:

使用 mocha,您可以尝试执行以下操作:

driver.findElements(By.xpath("//p[contains(text(), 'Welcome to the admin page!')]")).then(function(elements_arr)
    {
        if(elements_arr.length > 0){
            //your element got found
            console.log("Login Successful");
        }else{
            //your element was not found
            console.log("Login Unsuccessful");

            driver.findElements(By.xpath("//div[contains(text(), 'Invalid Credentials!')]")).then(function(elements_arr2)
            {
               if(elements_arr2.length > 0){
                  //your element invalid credentials got found
                  console.log("Login Unsuccessful, div invalid credentials found");
               }else{
                 //your element was not found
                 console.log("Login Unsuccessful, div invalid credentials not found");
               }
            });
        }         
    });