Selenium/Testng - 使用 testng.xml 中的参数时,IF 语句不起作用 [英] Selenium/Testng - IF statement not working when using parameter from testng.xml
问题描述
我使用 Selenium WebDriver 和 Testng(开始使用 .xml 文件)来测试使用多个浏览器的站点.
I'm using Selenium WebDriver with Testng (started using an .xml file) to test a site using multiple browsers.
我正在尝试创建一个方法,该方法将从 xml 文件中获取一个参数,并使用 IF 语句检测浏览器、创建相关驱动程序并返回它.
I'm trying to create a method which will take in a parameter from the xml file, and using an IF statement, detect the browser, create the relevant driver, and return it.
我遇到的问题是当我尝试将参数传递给方法时.例如,如果我通过Chrome",则 IF 语句工作正常并且驱动程序已创建.但是,如果我使用参数本身,则不会创建驱动程序,并且第一次使用时测试失败.
The trouble I'm having is when I try to pass through the parameter to the method. If I pass through "Chrome" for example, the IF statement works fine and the driver is created. However if I use the parameter itself, the driver isn't created and the test fails the first time it is used.
这是我正在使用的设置代码:
Here's the set up code I'm using:
@Parameters ({"driver_property_value","driver_property_location","browser"})
@BeforeClass
public void setUp(String driverPropertyValue, String driverPropertyLocation, String browser) throws Exception {
Setup setup = new Setup();
//set properties
System.setProperty(setup.driverPropertyValue(driverPropertyValue),setup.driverPropertyLocation(driverPropertyLocation));
driver = setup.driver(browser);
baseUrl = setup.baseURL();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
这是它的名字:
public WebDriver driver(String browser)
{
WebDriver value = null;
if (browser == "chrome")
{
value = new ChromeDriver();
}
return value;
}
这是我用来运行测试并传入参数的 testng xml:
and here's the testng xml that I'm using to run the tests and pass the parameters in:
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd">
<suite name = "testng" verbose="1">
<parameter name="driver_property_value" value="webdriver.chrome.driver"/>
<parameter name="driver_property_location" value="C:/chromedriver.exe"/>
<parameter name="browser" value="chrome"/>
<test name="chrome_tests">
<packages>
<package name="com.LoginPage"/>
</packages>
</test>
第一个设置似乎工作正常,只是驱动程序选择在使用参数时似乎不起作用.
The first setup seems to be working fine, it's just the the driver selection that doesn't seem to work when using a parameter.
任何帮助或建议将不胜感激.
Any help or advice would be appreciated.
谢谢
附言这是故障跟踪,不确定它是否会有所帮助.
p.s. here's a failure trace, not sure if it'll help or not.
java.lang.NullPointerException
at com.LoginPage.Login_Logout.setUp(Login_Logout.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:543)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:212)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
at org.testng.TestRunner.privateRun(TestRunner.java:753)
at org.testng.TestRunner.run(TestRunner.java:613)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1137)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1062)
at org.testng.TestNG.run(TestNG.java:974)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)
推荐答案
不要使用引用相等运算符 ==
来比较 String
s.使用 equals
或 equalsIgnoreCase
.
Do not use the reference equality operator ==
to compare String
s. Use equals
or equalsIgnoreCase
.
引用相等运算符检查两个操作数是否引用对象的同一实例.对于字符串和许多对象类型,这很少会像您期望的那样工作.
The reference equality operator checks that both operands refer to the same instance of an object. With Strings and many object types, this will rarely work the way you expect.
表达式 browser == "chrome"
解析为 false,因为即使 browser
变量具有值 "chrome"
,它也会很可能是表示 "chrome"
的字符串的不同实例.有关这意味着什么的更多详细信息,请参阅这个问题.
The expression browser == "chrome"
resolves to false because even if the browser
variable has the value "chrome"
, it will most likely be a different instance of the string representing "chrome"
. For much more detail on what this means, please refer to this question.
因此,当表达式解析为 false 时,driver
返回一个 null
,您的 setUp
可以愉快地使用它,就好像它是一个有效对象一样实例,导致 NullPointerException
.
So with that expression resolving to false, driver
returns a null
, which your setUp
proceeds to use happily as though it's a valid object instance, resulting in the NullPointerException
.
改变你的对比:
if (browser.equals("chrome")) {
value = new ChromeDriver();
}
此声明有多种变体.您可以使用 equalsIgnoreCase
来匹配任意字符大小写组合中的 "chrome"
,并且您可以颠倒文字 "chrome"
和browser
局部变量.如果 null
作为 browser
参数传入,这将防止在该行上发生 NullPointerException
.
There are several variations to this statement. You can use equalsIgnoreCase
to match "chrome"
in any combination of character cases, and you could reverse the order of the literal "chrome"
and the browser
local variable. What this would do is prevent a NullPointerException
from occurring on that line if null
was passed in as the browser
parameter.
if ("chrome".equalsIgnoreCase(browser)) {
value = new ChromeDriver();
}
这篇关于Selenium/Testng - 使用 testng.xml 中的参数时,IF 语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!