我试图通过使用此代码来比较两个数组,但我收到错误:java.lang.AssertionError

时间:2017-05-03 09:06:07

标签: java arraylist selenium-webdriver testng testng-eclipse

@Test(priority=4)
    public void content(){

        String[] x = {"Home", "Chatter", "Campsites", "Campsite Reservations", "Countries", "Table", "SPP Email Domain" };


        driver.findElement(By.xpath("//button[contains(@class,'salesforceIdentityAppLauncherHeader')]")).click();
        driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//*[@title='Content']")).click();
        driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);   
        WebElement Lightning = driver.findElement(By.xpath("//span[text()='Content']"));
        boolean displayedStatus = Lightning.isDisplayed();
        Assert.assertEquals(displayedStatus, true);
        List<WebElement> text = driver.findElements(By.xpath("//span[@class='slds-truncate'][contains(@data-aura-rendered-by,':0;p')]"));
        System.out.println(text.size());
        List<String> all_elements = new ArrayList<>();  
        for(int i=0; i<text.size();i++){
        all_elements.add(text.get(i).getText());
        }
        System.out.println(all_elements);
        Assert.assertEquals(all_elements, x);

但错误是:

  

失败:内容   java.lang.AssertionError:expected [[Ljava.lang.String; @ 25be7b63]但找到[[Home,Chatter,Campsites,Campsite Reservations,Countries,Table,SPP Email Domain]]

2 个答案:

答案 0 :(得分:0)

使用

List<String>转换为String[]
String[] converted_all_elements = all_elements.toArray(new String[0]); 

然后尝试一下,

Assert.assertArrayEquals( all_elements, x );

答案 1 :(得分:0)

您遇到此问题是因为您正在将字符串数组与ArrayList进行比较。

宣布你的&#39; x&#39;作为ArrayList,它应该工作:

列表x =新的ArrayList&lt;&gt;(Arrays.asList(&#34; Home&#34;,&#34; Chatter&#34;,&#34; Campsites&#34;,&#34; Campsite Reservations&# 34;,&#34;国家&#34;,&#34;表&#34;,&#34; SPP电子邮件域&#34;));