如何检查assert命令?

时间:2016-10-28 06:33:35

标签: selenium

我目前正在使用Selenium webdriver自动化网页。我现在卡住了。我想对我的脚本使用assert命令。以下是添加客户端模块的屏幕截图。

Enter image description here

输入客户端详细信息后,将转到另一个页面。在这里,我添加了一个客户名称," jason"。然后点击提交,在下一页我们可以看到客户列表。

Enter image description here

以下是页面来源:

 <ul id="rightMenu" class="right">
            <li class="topmenu rtopmenu-display-event">
        <a href="javascript:void(0)"><span class=" ico fa fa-user"></span>Hello Administrator</a>
        <ul>
          <li><a href="http://doc.local/profile"><span class="ico fa fa-user"></span><span>Profile</span></a></li>
          <li><a href="http://doc.local/password/change"><span class="ico fa fa-key" style ="margin-right:7px;"></span>Change Password</a></li>
          <li><a href="http://doc.local/logout"><span class="ico fa fa-sign-out"></span>Logout</a></li>
        </ul>
      </li>
    </ul>
      </ul>
</nav>
<div id="content">
  <div id="innerContent">
        <div class="bread-container">
      <ul class="bread-crumb">
                      <li>
         <a href="javascript:void(0)">
           Admin
         </a>
       </li>
                      <li>
         <a href="http://doc.local/admin/clients">
           Clients
         </a>
       </li>

       <li>
         <a href="javascript:void(0)" class="page-title">List Clients         </a>
       </li>
            </ul>
   </div>
   <div style="width:98%;" class="clearfix" id="search-container">
  <form class="form-inline">
    <div class="form-group">
      <input name="name" value="" size="13" placeholder="Company Name" class="form-control" />
    </div>
    <div class="form-group">
      <input name="group_name" value="" size="12" placeholder="Group Name" class="form-control" />
    </div>
    <input type="submit" value="Search" class="btn btn-info" />
  </form>
  <a href="http://doc.local/admin/clients/add" class=""><i title="Add Client" class="fa fa-plus-square add-plus"></i></a>
</div>
<div style="width:98%;" id="list-container">
  <ul class="li-table">
    <li class="clearfix title header">
      <span style="width:3%;">&nbsp;</span>
      <span style="width:6%;">Si.No</span>
      <span style="width:20%;" class="">
        <a href="http://doc.local/admin/clients?&sort=ASC&order_by=name">Name</a>
      </span>
      <span style="width:20%;" class="">
        <a href="http://doc.local/admin/clients?&sort=ASC&order_by=group_name">Group Name</a>
      </span>
      <span style="width:28%;" class="">
        <a href="http://doc.local/admin/clients?&sort=ASC&order_by=registered_on">Registered On</a>
      </span>
      <span style="width:16%;" class="">
        <a href="http://doc.local/admin/clients?&sort=ASC&order_by=status">Status</a>
      </span>
    </li>
            <li  class="clearfix li-row  ">
      <span style="width:3%;">
        <a href="javascript:void(0)">
          <i class="fa fa-arrow-circle-o-down show-actions"></i>
        </a>
        <ul class="list-actions">
          <li><a href="http://doc.local/admin/clients/edit/1?"><i class="fa fa-pencil c-black"></i>Edit</a></li>
          <li><a class="delete-item" href="javascript:void(0)" rel="http://doc.local/admin/clients/delete/1?"><i class="fa fa-trash"></i>Delete</a></li>
        </ul>
      </span>
      <span style="width:6%;">1.</span>
      <span style="width:20%;">Demo&nbsp;</span>
      <span style="width:20%;">TBI&nbsp;</span>
      <span style="width:28%;">2nd Oct'15&nbsp;</span>
      <span style="width:16%;">Active&nbsp;</span>
    </li>
        <li  class="clearfix li-row even ">
      <span style="width:3%;">
        <a href="javascript:void(0)">
          <i class="fa fa-arrow-circle-o-down show-actions"></i>
        </a>
        <ul class="list-actions">
          <li><a href="http://doc.local/admin/clients/edit/4?"><i class="fa fa-pencil c-black"></i>Edit</a></li>
          <li><a class="delete-item" href="javascript:void(0)" rel="http://doc.local/admin/clients/delete/4?"><i class="fa fa-trash"></i>Delete</a></li>
        </ul>
      </span>
      <span style="width:6%;">2.</span>
      <span style="width:20%;">Jason&nbsp;</span>
      <span style="width:20%;">Tbi&nbsp;</span>
      <span style="width:28%;">28th Oct'16&nbsp;</span>
      <span style="width:16%;">Active&nbsp;</span>
    </li>
            <li class="clearfix title footer">
      <span style="width:3%;">&nbsp;</span>
      <span style="width:6%;">Si.No</span>
      <span style="width:20%;" class="">
        <a href="http://doc.local/admin/clients?&sort=ASC&order_by=name">Name</a>
      </span>
      <span style="width:20%;" class="">
        <a href="http://doc.local/admin/clients?&sort=ASC&order_by=group_name">Group Name</a>
      </span>
      <span style="width:28%;" class="">
        <a href="http://doc.local/admin/clients?&sort=ASC&order_by=registered_on">Registered On</a>
      </span>
      <span style="width:16%;" class="">
        <a href="http://doc.local/admin/clients?&sort=ASC&order_by=status">Status</a>
      </span>
    </li>
  </ul>

这是测试脚本..

package Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class ClientPage
{
    private WebDriver driver;
    @FindBy(linkText="Admin")
    WebElement admin;
    @FindBy(linkText="Clients")
    WebElement clients;
    @FindBy(xpath="//*[@id='search-container']/a/i")
    WebElement search;
    @FindBy(id="name")
    WebElement name;
    @FindBy(id="groupname")
    WebElement groupname;
    @FindBy(id="address")
    WebElement address;
    @FindBy(id="status")
    WebElement status_dropdown;
    @FindBy(className="btn-primary")
    WebElement button;

    // Constructor
    public ClientPage(WebDriver driver)
    {
        PageFactory.initElements(driver, this);
    }
    public void set_Admin()
    {
        admin.click();
    }
    public void set_Client()
    {
        clients.click();
    }
    public void set_Search()
    {
        search.click();
    }
    public void set_Name(String nam)
    {
        name.clear();
        name.sendKeys(nam);
    }
    public void set_Groupname(String grp)
    {
        groupname.clear();
        groupname.sendKeys(grp);
    }
    public void set_Address(String add)
    {
        address.clear();
        address.sendKeys(add);
    }
    public void select_List(String status)
    {
        Select statusdropdown=new Select(status_dropdown);
        statusdropdown.selectByVisibleText(status);
    }
    public void set_Button()
    {
        button.submit();
    }
}


package Test;
import org.testng.annotations.Test;

import Pages.ClientPage;
import Pages.LoginPage;
import Pages.RolesPage;

import org.testng.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class ClientTest
{
    WebDriver driver;
    @BeforeTest
    public void setup()
    {
        System.setProperty("webdriver.firefox.marionette","pathToGeckodriver");
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://doc.loc");
    }
    @Test(priority=1)
    public void verify1()
    {
        LoginPage login=new LoginPage(driver);
        login.set_username("docm");
        login.set_password("1");
        login.click_button();
        Assert.assertTrue(driver.getPageSource().contains("Hello Administrator"));
    }
    @Test(priority=2)
    public void verif2()
    {
        ClientPage client=new ClientPage(driver);
        client.set_Admin();
        client.set_Client();
        client.set_Search();
        client.set_Name("Jason");
        client.set_Groupname("Tbi");
        client.set_Address("Ernakulam");
        client.select_List("active");
        client.set_Button();
    Assert.
    }
}

1 个答案:

答案 0 :(得分:0)

首先,通常会让你的setup()方法进行登录,并包含一个可以注销的拆机()。这使每个测试用例独立于其他测试用例。

接下来,您需要为客户列表定义一个页面。此页面将包含一个方法findAllClients,它返回与客户端列表中的行匹配的WebElements列表。完成后,您可以在verif2测试中使用它来检查包含您在创建客户端时传入的数据的行。