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:\程序 Files\JetBrains\IntelliJ IDEA 2018.1.1\插件\testng\lib\testng-plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\程序 文件\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\程序 文件\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\extashorn.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\程序 文件\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\程序 文件\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\程序 Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\程序 文件\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\程序 文件\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\程序 文件\Java\jdk1.8.0_131\jre\lib\rt.jar;C:\Users\jashan\IdeaProjects\SeleniumJava\out\production\SeleniumJava;H:\selenium-server-standalone-3.11.0.jar;C:\Users\jashan.m2\repository\org\junit\jupiter\junit-jupiter-api\5.0.0\junit-jupiter-api-5.0.0.jar;C:\Users\jashan.m2\repository\org\apiguardian\apiguardian-api\1.0.0\apiguardian-api-1.0.0.jar;C:\Users\jashan.m2\repository\org\opentest4j\opentest4j\1.0.0\opentest4j-1.0.0.jar;C:\Users\jashan.m2\repository\org\junit\platform\junit-platform-commons\1.0.0\junit-platform-commons-1.0.0.jar;C:\程序 Files\JetBrains\IntelliJ IDEA 2018.1.1\插件\testng\lib\testng.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.1\plugins\testng\lib\jcommander.jar" org.testng.RemoteTestNGStarter -usedefaultlisteners false -socket53637 @w@C:\Users\jashan\AppData\Local\Temp\idea_working_dirs_testng.tmp -temp C:\Users\jashan\AppData\Local\Temp\idea_testng.tmp [TestNG] Running:
=================================================================================================================================================================
C:\Users\jashan.IntelliJIdea2018.1\system\temp-testng-customsuite.xml 启动ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) 在端口 18144 上 仅限本地 允许连接。2018年5月10日 凌晨 12点46分55秒 org.openqa.selenium.remote.ProtocolHandshake createSession INFO: 检测到的方言:OSS 实际消息 * 允许的最大数量购买为 500.java.lang.断言错误:预期 [* 最大值 允许购买的数量为 500。但发现 [* 最大值 允许购买的数量为 500。] 预期:*最大值 允许购买的数量为 500。实际:* 最大数量 允许购买的是 500。在 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.测试案例3(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) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:348) atorg.testng.SuiteRunner.runSequentially(SuiteRunner.java:343) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305) 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 org.testng.TestNG.runSuitesLocal(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123) =========================================================================================================================================================================================================== ===
*
您的测试表现正常;由于同一测试中有多个断言,因此测试失败,并在第一个断言失败时停止执行。
如果您的测试包含多个断言,并且您希望评估所有断言,则可能需要考虑在测试中使用 SoftAsserts。
SoftAssert softAssert = new SoftAssert();
softAssert.assertTrue(false);
softAssert.assertEquals("a", "b");
softAssert.assertAll();