我使用这个https://www.browserstack.com/docs/automate/selenium/view-test-results/mark-tests-as-pass-fail作为参考。但是我不能让REST API显示"失败"。我可以知道我的问题是什么吗?我还附上了文本日志,其中显示"运行JavaScript……",正确吗?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.JavascriptExecutor;
public class test1 {
public WebDriver driver = null;
public static final String USERNAME = "";
public static final String AUTOMATE_KEY = "";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
@BeforeClass
public void setup() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "80");
caps.setCapability("name", "Test1");
driver = new RemoteWebDriver(new URL(URL), caps);
}
@Test (priority = 1)
public void test_1() {
driver.get("https://youtube.com");
String actualUrl = "https://www.youtube.com/";
String expectedUrl = "https://www.youtube.com/";
Assert.assertEquals(actualUrl, expectedUrl);
JavascriptExecutor jse = (JavascriptExecutor)driver;
if (actualUrl.equals(expectedUrl)) {
jse.executeScript("browserstack_executor: {"action": "setSessionStatus", "arguments": {"status": "passed", "reason": "Url matched!"}}");
}
else {
jse.executeScript("browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Url not matched"}}");
}
}
@Test (priority = 2)
public void test_2() {
driver.get("https://google.com");
String actualUrl = "https://www.google.com/";
String expectedUrl = "https://www.youtube1.com/";
Assert.assertEquals(actualUrl, expectedUrl);
JavascriptExecutor jse = (JavascriptExecutor)driver;
if (actualUrl.equals(expectedUrl)) {
jse.executeScript("browserstack_executor: {"action": "setSessionStatus", "arguments": {"status": "passed", "reason": "Url matched!"}}");
}
else {
jse.executeScript("browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Url not matched"}}");
}
}
@AfterClass
public void tearDown() {
driver.quit();
}
}
我测试了youtube和谷歌网址。
Assert.assertEquals(actualUrl, expectedUrl);
不是软断言,它将停止命令执行,因此测试不会到达api行。
将其从测试中删除或使用软断言。
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(actualUrl, expectedUrl)