Selenium Assert错误来自intellij Idea

时间:2018-05-09 19:34:05

标签: selenium selenium-webdriver selenium-chromedriver

package Ecommerce;

import org.omg.CORBA.TIMEOUT;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.testng.Assert.assertEquals;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/*This is my Code
 * www.github.com/jashangunike
 * Author-Jashandeep Singh */

public class TC03 {

    static public WebDriver driver = null;
    public String baseUrl = "http://live.guru99.com/index.php/";

    @Test
    public void TestCase3() {

        driver.navigate().to(baseUrl);

        //Click on mobile link
        driver.findElement(By.xpath("//a[@href='http://live.guru99.com/index.php/mobile.html']")).click();
        //Click on Add to cart
        driver.findElement(By.xpath("//*[@id=\"top\"]/body/div/div/div[2]/div/div[2]/div[1]/div[3]/ul/li[1]/div/div[3]/button/span/span")).click();
        // Click on Qty Box
        driver.findElement(By.xpath("//*[@id=\"shopping-cart-table\"]/tbody/tr/td[4]/input")).click();
        // Clear
        driver.findElement(By.xpath("//*[@id=\"shopping-cart-table\"]/tbody/tr/td[4]/input")).clear();
        // put 1000 units
        driver.findElement(By.xpath("//*[@id=\"shopping-cart-table\"]/tbody/tr/td[4]/input")).sendKeys("1000");
        // Click on Update button
        driver.findElement(By.xpath("//*[@id=\"shopping-cart-table\"]/tbody/tr/td[4]/button/span/span")).click();

        // Verification of error message
        String ExpectedMSG = "* The maximum quantity allowed for purchase is 500. ";
        String ActualMSG = driver.findElement(By.xpath("//*[@id=\"shopping-cart-table\"]/tbody/tr/td[2]/p")).getText();
        System.out.println("Actual message "+ ActualMSG);

        try {

            assertEquals(ExpectedMSG,ActualMSG);

        }
        catch (Exception e){

        e.printStackTrace();
}

        //Verifying message after emptying the cart

        driver.findElement(By.id("empty_cart_button")).click();
        String noItemsStg = ("You have no items in your shopping cart.");
        String noItemsMsg = driver.findElement(By.xpath(".//*[@id='top']/body/div[1]/div/div[2]/div/div/div[2]/p[1]")).getText();
        System.out.println("You have no items msg = " + noItemsMsg);

        try {

            assertEquals(noItemsStg,noItemsMsg);
        }

        catch(Exception ex) {
            ex.printStackTrace();

            }
    }

    @BeforeMethod
    public void BeforeMethod() {
        System.setProperty("webdriver.chrome.driver" , "H:\\software\\Selenium & Java & Components\\drivers\\chromedriverlat\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    }

    @AfterMethod
    public void AfterMethod() {
         driver.quit();
    }
}

我的第二次尝试和捕获声明不起作用。问题出在哪儿?即验证空车。显示一个结果,但未显示其他结果。我已经把显示的那个加粗了。

调试代码后出现错误

*

  

" C:\ Program Files \ Java \ jdk1.8.0_131 \ bin \ java.exe" -ea   -Didea.test.cyclic.buffer.size = 1048576" -javaagent:C:\ Program Files \ JetBrains \ IntelliJ IDEA   2018.1.1 \ lib \ idea_rt.jar = 53638:C:\ Program Files \ JetBrains \ IntelliJ IDEA 2018.1.1 \ bin" -Dfile.encoding = UTF-8 -classpath" C:\ Program   Files \ JetBrains \ IntelliJ IDEA 2018.1.1 \ lib \ idea_rt.jar; C:\ Program   Files \ JetBrains \ IntelliJ IDEA   2018.1.1 \ plugins \ testng \ lib \ testng-plugin.jar; C:\ Program Files \ Java \ jdk1.8.0_131 \ jre \ lib \ charsets.jar; C:\ Program   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \ deploy.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\访问桥64.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ cldrdata.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ dnsns.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ jaccess.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ jfxrt.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ localedata.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ nashorn.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ sunec.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\程序(sunjce_provider.jar); C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ sunmscapi.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ sunpkcs11.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ lib中\分机\ zipfs.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \ javaws.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \ jce.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \ jfr.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \ jfxswt.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \ jsse.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \管理-agent.jar中; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \ plugin.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \ resources.jar; C:\ PROGRAM   文件\的Java \ jdk1.8.0_131 \ JRE \ LIB \ rt.jar的; C:\用户\ jashan \ IdeaProjects \ SeleniumJava \出\生产\ SeleniumJava; H:\硒服务器独立-3.11.0.jar; C ^ :\用户\ jashan.m2 \库\有机\的junit \木星\的junit-木星-API \ 5.0.0 \的junit-木星-API-5.0.0.jar; C:\用户\ jashan.m2 \库\有机\ apiguardian \ apiguardian-API \ 1.0.0 \ apiguardian-API-1.0.0.jar; C:\用户\ jashan.m2 \库\有机\ opentest4j \ opentest4j \ 1.0.0 \ opentest4j-1.0.0.jar; C:\ Users \用户jashan.m2 \库\组织\的JUnit \平台\ JUnit的平台,公共\ 1.0.0 \ JUnit的平台 - 公共-1.0.0.jar; C:\ PROGRAM   Files \ JetBrains \ IntelliJ IDEA   2018.1.1 \ plugins \ testng \ lib \ testng.jar; C:\ Program Files \ JetBrains \ IntelliJ IDEA   2018年1月1日\插件\ TestNG的\ lib中\ jcommander.jar" org.testng.RemoteTestNGStarter -usedefaultlisteners false -socket53637   沃顿知识在线@ C:\用户\ jashan \应用程序数据\本地的\ Temp \ idea_working_dirs_testng.tmp   -temp C:\ Users \ jashan \ AppData \ Local \ Temp \ idea_testng.tmp [TestNG]正在运行:
  C:\ Users \用户jashan.IntelliJIdea2018.1 \ SYSTEM \ TEMP-TestNG的-customsuite.xml   启动ChromeDriver 2.38.552522   (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb)on port 18144 Only local   允许连接。 2018年5月10日上午12:46:55   org.openqa.selenium.remote.ProtocolHandshake createSession INFO:   检测到的方言:OSS实际消息*允许的最大数量   购买为500. java.lang.AssertionError:expected [*最大值   允许购买的数量是500.]但发现[*最大值   允许购买的数量是500.]预计:*最大值   允许购买的数量是500.实际:*最大数量   允许购买是500。 at   org.testng.Assert.fail(Assert.java:94)at   org.testng.Assert.failNotEquals(Assert.java:494)at   org.testng.Assert.assertEquals(Assert.java:123)at   org.testng.Assert.assertEquals(Assert.java:176)at   org.testng.Assert.assertEquals(Assert.java:186)at   电子商务.TC03.TestCase3(TC03.java:50)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)     在org.testng.internal.Invoker.invokeMethod(Invoker.java:714)at   org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)at at   org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)at at   org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)     在   org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)     在org.testng.TestRunner.privateRun(TestRunner.java:767)at   org.testng.TestRunner.run(TestRunner.java:617)at   org.testng.SuiteRunner.runTest(SuiteRunner.java:348)at   org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)at at   org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)at at   org.testng.SuiteRunner.run(SuiteRunner.java:254)at   org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at   org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at   org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)at at   org.testng.TestNG.runSuitesLocally(TestNG.java:1149)at at   org.testng.TestNG.run(TestNG.java:1057)at   org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)at at   org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)   ===============================================默认套件总计测试运行:1,失败:1,跳过:0   ===============================================完成过程退出代码0

*

1 个答案:

答案 0 :(得分:0)

你的测试表现应该如此;由于在同一测试中有多个断言,测试失败并在第一个断言失败时停止执行。

如果您的测试包含多个断言并且您希望评估所有断言,则可能需要在测试中使用SoftAsserts。 e.g。

SoftAssert softAssert = new SoftAssert();

softAssert.assertTrue(false);
softAssert.assertEquals("a", "b");

softAssert.assertAll();