硒的断言-TestNG

时间:2019-03-06 06:03:05

标签: selenium

我正在尝试使用断言检查图像中突出显示的值是否与我提供的值相同:

snap

下面是代码:

WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element1 = wait
    .until(ExpectedConditions.visibilityOfElementLocated(By
        .xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div/ion-card/ion-row[1]/ion-col/ion-row[2]")));
String element = driver
    .findElement(
        By.xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div/ion-card/ion-row[1]/ion-col/ion-row[2]"))
    .getText();

Assert.assertEquals("10:45", element);

但是我遇到了以下错误:

Exception in thread "main" java.lang.AssertionError: expected [11:21 AM  to  Clock-out
In Progress] but found [10:45]
    at org.testng.Assert.fail(Assert.java:96)
    at org.testng.Assert.failNotEquals(Assert.java:776)
    at org.testng.Assert.assertEqualsImpl(Assert.java:137)
    at org.testng.Assert.assertEquals(Assert.java:118)
    at org.testng.Assert.assertEquals(Assert.java:453)
    at org.testng.Assert.assertEquals(Assert.java:463)
    at pulse.tpr.LoginInterface.rightNowToClockout(LoginInterface.java:172)
    at pulse.tpr.LoginInterface.main(LoginInterface.java:182)

我是Selenium和Java的新手,所以可以提供任何帮助。

3 个答案:

答案 0 :(得分:1)

尝试将Assert.assertTruecontains一起使用。

Assert.assertTrue(element.contains("10:45"));

通过这种方式将检查文本“ 10:45”是否存在元素文本字符串。

答案 1 :(得分:1)

在您的代码中

driver.findElement(By.xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div/ion-card/ion-row[1]/ion-col/ion-row[2]")).getText();

将值 11:21 AM返回到正在进行的时钟,您正在检查其是否等于 10:45 。因此很明显它将失败。甚至连您连接的屏幕截图都将 11:45设置为“超时”

如果需要通过,则必须检查时间。然后可能应该如下所示:

WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element1 = wait
    .until(ExpectedConditions.visibilityOfElementLocated(By
        .xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div/ion-card/ion-row[1]/ion-col/ion-row[2]")));
String element = driver
    .findElement(
        By.xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div/ion-card/ion-row[1]/ion-col/ion-row[2]"))
    .getText();

Assert.assertTrue(element.conatins("11:21"));

在这里,我们正在检查 11:21到“正在进行中的时钟” 是否包含文本 11:21 ,以使其通过。如果您需要 10:45 并显示 11:21 ,则可能是有效的失败。

答案 2 :(得分:0)

此问题已解决。但是我的TestNG输出显示NullPointer错误: enter image description here

package pulse.tpr;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.Test;

public class LoginInterface {

    public WebDriver driver;
    public Actions actions;

    public void launchApplication() {

        System.setProperty("webdriver.chrome.driver",
                "C:\\MyChromeDriver\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("mysite");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
        actions = new Actions(driver);

    }

    public void closeApplication() {
        driver.close();
    }

    public void adminLogin() {

        actions.moveToElement(driver.findElement(By.name("userId")));
        actions.click();
        actions.sendKeys("sysadmin");
        actions.build().perform();

        actions.pause(java.time.Duration.ofSeconds(1));

        actions.moveToElement(driver.findElement(By.name("password")));
        actions.click();
        actions.sendKeys("MMNext13#");
        actions.build().perform();

        actions.pause(java.time.Duration.ofSeconds(1));

        actions.moveToElement(driver.findElement(By.name("dcNumber")));
        actions.click();
        actions.sendKeys("7036");
        actions.build().perform();
    }

    public void tprPage() {

        actions.moveToElement(driver.findElement(By
                .cssSelector("body > ion-app > ng-component > ion-nav > page-login > ion-content > div.scroll-content > ion-card > ion-grid > form > ion-list > div.login-button > button > span")));
        actions.click();
        actions.build().perform();

        actions.moveToElement(driver.findElement(By.id("tab-t0-2")));
        actions.click();
        actions.build().perform();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

    public void createMove() {
        actions.moveToElement(driver.findElement(By
                .className("send-associates-button")));
        actions.click();
        actions.build().perform();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        actions.moveToElement(driver.findElement(By
                .cssSelector("#tabpanel-t0-2 > send-associate-page > ion-content > div.scroll-content > div > ion-list > ion-item:nth-child(4)")));
        actions.click();
        actions.build().perform();

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='footers']/ion-toolbar/div[2]/ion-row/ion-col[2]/button")));
        actions.click();
        actions.build().perform();

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]")));
        actions.click();
        actions.build().perform();

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]/ion-row[2]")));
        actions.click();
        actions.build().perform();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        // Select to area

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]")));
        actions.click();
        actions.build().perform();

        // Select to area

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-col[2]/ion-row[2]")));
        actions.click();
        actions.build().perform();

        // Next button in to area page

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
        actions.click();
        actions.build().perform();
    }

    @Test
    public void rightNowToClockout() {

        // Right Now Time

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[4]")));
        actions.click();
        actions.build().perform();

        // Next Button

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
        actions.click();
        actions.build().perform();

        // Clockout time

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[2]/ion-col")));
        actions.click();
        actions.build().perform();

        // Next button

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/button")));
        actions.click();
        actions.build().perform();

        // Send Button

        actions.moveToElement(driver.findElement(By
                .xpath("//*[@id='tabpanel-t0-2']/tpr-send-associates-page/ion-footer/ion-row/ion-row")));
        actions.click();
        actions.build().perform();

        WebDriverWait wait = new WebDriverWait(driver, 60);
        WebElement element1 = wait
                .until(ExpectedConditions.visibilityOfElementLocated(By
                        .xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div/ion-card/ion-row[1]/ion-col/ion-row[2]")));
        String element = driver
                .findElement(
                        By.xpath("//*[@id='tabpanel-t0-2']/tpr-summary-page/ion-content/div[2]/div/div[2]/div[1]/ion-card/ion-row[1]/ion-col/ion-row[2]/div[1]/label[1]"))
                .getText();
        Assert.assertTrue(element.contains("1:35"));
    }

    public static void main(String[] args) {

        LoginInterface obj = new LoginInterface();
        obj.launchApplication();
        obj.adminLogin();
        obj.tprPage();
        obj.createMove();
        obj.rightNowToClockout();

    }

}

这是我的代码段。