如何使用在selenium webdriver中使用assert列表

时间:2017-10-18 06:41:15

标签: java selenium webdriver assert

我正在尝试使用Assert验证FB登录页面上显示的月份。

但是我得到的输出不正确。

以下是代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

5 个答案:

答案 0 :(得分:0)

使用以下代码

<header id="masthead" class="site-header">

  <div class="site-branding">
    <div class="site-branding-text">
      <p class="site-title"><a href="http://localhost/wordpress/" rel="home">Treehouse</a></p>
      <p class="site-description">Its an awesome website</p>
    </div>
  </div>

  <!-- .site-branding -->
  <nav id="site-navigation" class="main-navigation nav2">
    <div class="menu-toggle">
      <div id="sidebar-btn">
        <span></span>
        <span></span>
        <span></span>
      </div>
    </div>
    <div class="menu-primary-menu-links-container">
      <ul id="primary-menu" class="menu">
        <li id="menu-item-167" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home menu-item-167"><a href="http://localhost/wordpress/">Home</a></li>
        <li id="menu-item-165" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-165"><a href="http://localhost/wordpress/about/">About</a></li>
        <li id="menu-item-1987" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-218 current_page_item menu-item-has-children menu-item-1987"><a href="http://localhost/wordpress/gallery/">Gallery</a>
          <button class="dropdown-toggle" aria-expanded="false"><span class="dropdown-symbol">+</span><span class="screen-reader-text">Expand child menu</span></button>
          <ul class="sub-menu">
            <li id="menu-item-1988" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1988"><a href="http://localhost/wordpress/book-a-workshop/">Book A Workshop</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </nav>

  <!-- #site-navigation -->
  <nav id="site-navigation" class="main-navigation nav1">
    <div class="menu-toggle">
      <div id="sidebar-btn">
        <span></span>
        <span></span>
        <span></span>
      </div>
    </div>
    <div class="menu-primary-menu-links-container">
      <ul id="primary-menu" class="menu">
        <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home menu-item-167"><a href="http://localhost/wordpress/">Home</a></li>
        <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-165"><a href="http://localhost/wordpress/about/">About</a></li>
        <li class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-218 current_page_item menu-item-has-children menu-item-1987"><a href="http://localhost/wordpress/gallery/">Gallery</a>
          <button class="dropdown-toggle" aria-expanded="false"><span class="dropdown-symbol">+</span><span class="screen-reader-text">Expand child menu</span></button>
          <ul class="sub-menu">
            <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1988"><a href="http://localhost/wordpress/book-a-workshop/">Book A Workshop</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </nav>
  <!-- #site-navigation -->
</header>

答案 1 :(得分:0)

@Sam:请找到下面附带的代码段:

        for(int count =0 ; count < month_list.size(); count++)
    {
        WebElement ele = month_dd.getOptions().get(count); 
        System.out.println(ele.getText());
        for(int i=0;i<exp.length;i++){
            if(ele.getText().toLowerCase().equals(exp[i].toLowerCase())){
                System.out.println("Matched ");

            }
        }
    }

仅供参考:ID为月份的选项的提取值如下 - 月,一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月 如果要比较实际字符串,可以删除.toLowerCase()函数。

答案 2 :(得分:0)

替换代码中的以下代码段:

WebElement month_dropdown= driver.findElement(By.id("month"));
Select month_dd = new Select(month_dropdown);
List<WebElement> month_list = month_dd.getOptions();
int total_months = month_list.size();
System.out.println("Total month Count is = " +total_months);
String[] exp = {"Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; 

for (int i=0;i<total_months;i++)
{

    // Use Assertion you yow want to abort execution if value is not expected
             //assertEquals(month_list.get(i).getText().toUpperCase(), exp[i].toUpperCase());

    if(month_list.get(i).getText().equalsIgnoreCase(exp[i]))
    {
        System.out.println("Matching value : "+month_list.get(i).getText()+" : : "+exp[i] );
    }
    else
    {
        System.out.println("Non Matching value : "+month_list.get(i).getText()+" : : "+exp[i] );
    }

}

答案 3 :(得分:0)

您需要处理以下几项事项:

  1. Firefox Browser以最大化模式打开,因此您可以腾出driver.manage().window().maximize();
  2. 您要将month_dd.getOptions();存储在List<WebElement>中,以便稍后比较这些值,我们会使用List<String>代替String[]数组。
  3. 初始化单个迭代器以遍历List
  4. 在比较文字时,而不是equals使用equalsIgnoreCase来处理区分大小写。
  5. 以下是您自己的代码,其中包含一些简单的调整:

    public class Assert_List 
    {
            @Test
            public void selectByvalue() throws InterruptedException
            {
                System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
                WebDriver driver = new FirefoxDriver();
                driver.get("https://www.facebook.com/");
                WebElement month_dropdown= driver.findElement(By.id("month"));
                Select month_dd = new Select(month_dropdown);
                List<WebElement> month_list = month_dd.getOptions();
                System.out.println("Total month_list Count is = " +month_list.size());
                List<String> month = Arrays.asList("Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
                for(int i=0; i<month_list.size(); i++)
                {
                    if(month_list.get(i).getText().equalsIgnoreCase(month.get(i)))
                        System.out.println("Matched");
                    else
                        System.out.println("Didn't Match");
                }
            }
    }
    
  6. 控制台输出如下:

        Total month_list Count is = 13
        Matched
        Matched
        Matched
        Matched
        Matched
        Matched
        Matched
        Matched
        Matched
        Didn't Match
        Matched
        Matched
        Matched
        PASSED: selectByvalue
    
        ===============================================
            Default test
            Tests run: 1, Failures: 0, Skips: 0
        ===============================================
    
    
        ===============================================
        Default suite
        Total tests run: 1, Failures: 0, Skips: 0
        ===============================================   
    
      

    Note :在https://www.facebook.com/ {月} Sept ,我们的List包含 { {1}} 即可。因此未匹配

答案 4 :(得分:0)

如果你不想使用太多的循环,你可以使用该集合。

首先将String数组转换为ArrayList。并创建一个包含预期ArrayList

的新列表
List<String> months = new ArrayList<String>(Arrays.asList("Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"));
List<String> actualMonths = new ArrayList<String>();


List<WebElement> month_list = month_dd.getOptions();
for (WebElement ele: month_list){
    actualMonths.add(ele.getText());

}

如果订单无关紧要,请在列表中应用排序。如果订单很重要,您甚至不需要应用排序。

Collections.sort(months);
Collections.sort(actualMonths);
Assert.assertTrue(months.equals(actualMonths));