我曾尝试System.setProperty在 main 方法中使用,没有任何问题,但是当我切换到TestNGSelenium 学习的一部分时,我意识到我们无法System.setProperty在类级别编写。它应该位于方法级别或位于static块中。我只是想了解 Java 的什么特性迫使我们这样做。
public class NewTest {
public String baseUrl = "http://newtours.demoaut.com/";
static {
System.setProperty("webdriver.chrome.driver","D:\\paths\\chromedriver.exe");
}
WebDriver driver = new ChromeDriver();
@Test
public void f1() {
...}
}
Run Code Online (Sandbox Code Playgroud)
在静态块之外写入此内容会显示编译错误,例如“此行有多个标记,语法错误”
我将 testNG 与 IntelliJ 结合使用,并且尝试设置一个 XML 文件以控制测试用例的顺序(除其他外)。但是,当我尝试将类名放入 XML 中时,它无法识别。我在某处读到它需要位于包中,因此我为测试创建了一个包,但它仍然无法识别包或类。它给我的错误是无法解析类 BotIncidentTest
这是我的 XML 文件:
<test name="everything">
<classes>
<class name="test.BotIncidentTest" />
</classes>
</test>
Run Code Online (Sandbox Code Playgroud)
先谢谢您的帮助 :)
我正在尝试System.lineSeparator()在 Java testng 测试用例中使用来构建expectedExceptionsMessageRegExp如下。
@Test(description = "When path parameter has given unmatch data type in ballerina",
expectedExceptions = BallerinaOpenApiException.class,
expectedExceptionsMessageRegExp = "File has errors: " + System.lineSeparator() + "Semicolon is missing at line 45")
public void testMissionSemicolon() throws IOException, BallerinaOpenApiException {
// test implementation
}
Run Code Online (Sandbox Code Playgroud)
我在 处遇到以下错误expectedExceptionsMessageRegExp。
Attribute value must be constant
Run Code Online (Sandbox Code Playgroud)
出现这个错误的原因是什么?我怎样才能达到我的目的?
从 JsonPath 对象中提取数据时得到 null,下面是 json 响应数据
{
"status": "OK",
"header": {
"headerAttributes": {}
},
"errors": [],
"payload": {
"totalCount": 0,
"returnTerms": []
}
}
Run Code Online (Sandbox Code Playgroud)
从“totalCount”中提取值的java方法
public void getjsonValue() {
JsonPath jsonPathEvaluator = response.jsonPath();
System.out.println(jsonPathEvaluator.get("$['payload']['totalCount']"));
}
Run Code Online (Sandbox Code Playgroud) 我已经成功安装了 TestNG 并在 eclipse (4.9.0) 中进行了确认。但是,在创建没有主类的基本类时,我在注释 @Test 处收到错误。即使导入测试注释包后,包上也存在错误。并且错误表明无法解决。 在此输入图像描述
我经常使用 testNG 的 assertTrue 来验证交易是否正确
public void verifyAmount(WebElement element, String someText){
assertTrue(element.getText().contains(someText));
}
Run Code Online (Sandbox Code Playgroud)
当它失败时,它说
java.lang.AssertionError: did not expect to find [true] but found[false]
Run Code Online (Sandbox Code Playgroud)
是否可以更改断言错误以说明究竟出了什么问题,而不仅仅是真/假陈述?是否可以使该消息更具体?有没有办法让断言错误说:
java.lang.AssertionError: did not expect to find [10.000 $] but found[3000 $]
Run Code Online (Sandbox Code Playgroud) 代码如下:
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class ContactPageElements {
public static WebElement element = null;
public static String baseURL1 = "http://something.com";
//Clicking logo should take you back to the baseURL
public static void clickLogo (WebDriver driver) {
element = driver.findElement(By.xpath(".//*[@id='blah'"));
element.click();
String currentURL = driver.getCurrentUrl();
assert.assertEquals(currentURL, baseURL1);
}
}
Run Code Online (Sandbox Code Playgroud)
对于断言,我收到错误:“类型不匹配:无法从 void 转换为布尔值”