如何解决如何在单独的Java类-Selenium
我正在尝试创建一个新类,其中将包含所有用于Selenium测试的变量。下面是类:
类别:variablesRepo:
package src;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class variablesRepo {
// public static final WebElement ChkBox = null;
static WebDriver driver = new ChromeDriver();
static WebElement LoginButton = driver.findElement(By.id("btnLogin"));
}
NewTest.Java类:
package src;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterSuite;
public class NewTest {
WebDriver Driver = variablesRepo.driver;
@Test
public void TestElement() {
WebElement LoginButton = variablesRepo.LoginButton;
assert LoginButton.isDisplayed();
}
@BeforeSuite
public void beforeSuite() {
Driver.get("https://opensource-demo.orangehrmlive.com/");
}
@AfterSuite
public void afterSuite() {
Driver.close();
}
}
当我尝试运行测试时,浏览器将打开,并且页面也会打开,因此脚本能够从veriableRepo中检索变量。但是,当涉及到Web元素时,脚本将失败。似乎正在尝试在Repo文件中找到Webelements。我收到以下错误:
由以下原因引起:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“方法”:“ css选择器”,“选择器”:“#btnLogin”}
我们如何解决此问题并从Repo类中导入变量?
更新:我得到的错误堆栈是:
[RemoteTestNG]检测到TestNG版本7.3.0,正在启动ChromeDriver 85.0.4183.87(cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs / branch-heads / 4183 @ {#1689}) 在端口25606上仅允许本地连接。请参阅 https://chromedriver.chromium.org/security-considerations的 有关保护ChromeDriver安全的建议。 ChromeDriver已启动 成功。 2020年9月2日上午11:13:42 org.openqa.selenium.remote.ProtocolHandshake createSession INFO: 检测到的方言:W3C org.testng.TestNGException:无法实例化 src.NewTest类,位于 org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30) 在 org.testng.internal.InstanceCreator.instantiateUsingDefaultConstructor(InstanceCreator.java:193) 在 org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory(InstanceCreator.java:113) 在 org.testng.internal.InstanceCreator.createInstance(InstanceCreator.java:79) 在 org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:109) 在org.testng.internal.ClassImpl.getInstances(ClassImpl.java:167)在 org.testng.TestClass.getInstances(TestClass.java:102)在 org.testng.TestClass.initTestClassesAndInstances(TestClass.java:82) 在org.testng.TestClass.init(TestClass.java:74)处 org.testng.TestClass。(TestClass.java:39)在 org.testng.TestRunner.initMethods(TestRunner.java:457)在 org.testng.TestRunner.init(TestRunner.java:336)在 org.testng.TestRunner.init(TestRunner.java:289)在 org.testng.TestRunner。(TestRunner.java:220)在 org.testng.remote.support.RemoteTestNG6_12 $ 1.newTestRunner(RemoteTestNG6_12.java:33) 在 org.testng.remote.support.RemoteTestNG6_12 $ DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66) 在 org.testng.ITestRunnerFactory.newTestRunner(ITestRunnerFactory.java:55) 在 org.testng.SuiteRunner $ ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:676) 在org.testng.SuiteRunner.init(SuiteRunner.java:178)处 org.testng.SuiteRunner。(SuiteRunner.java:112)在 org.testng.TestNG.createSuiteRunner(TestNG.java:1306)在 org.testng.TestNG.createSuiteRunners(TestNG.java:1282)在 org.testng.TestNG.runSuitesLocally(TestNG.java:1131)位于 org.testng.TestNG.runSuites(TestNG.java:1069)在 org.testng.TestNG.run(TestNG.java:1037)在 org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115) 在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)处引起 创建人:java.lang.reflect.InvocationTargetException sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知 来源) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 源)位于java.lang.reflect.Constructor.newInstance(未知源) 在 org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23) ... 27更多原因于:java.lang.ExceptionInInitializerError at src.NewTest。(NewTest.java:13)...还有32个原因: org.openqa.selenium.NoSuchElementException:否这样的元素:无法 定位元素:{“方法”:“ css选择器”,“选择器”:“#btnLogin”}
(会话信息:chrome = 85.0.4183.83)有关此错误的文档, 请拜访: https://www.seleniumhq.org/exceptions/no_such_element.html构建信息: 版本:“ 3.141.59”,修订版:“ e82be7d358”,时间: '2018-11-14T08:25:48'系统信息:主机:'DML-02213',ip:'10 .9.9.5', os.name:“Windows 10”,os.arch:“ amd64”,os.version:“ 10.0”, java.version:“ 1.8.0_261”驱动程序信息: org.openqa.selenium.chrome.ChromeDriver功能 {acceptInsecureCerts:否,browserName:chrome,browserVersion: 85.0.4183.83,chrome:{chromedriverVersion:85.0.4183.87(cd6713ebf92fa ...,userDataDir:C:\ Users \ nhassan \ AppData \ Lo ...}, goog:chromeOptions:{debuggerAddress:localhost:62831}, javascriptEnabled:true,networkConnectionEnabled:false, pageLoadStrategy:正常,平台:WINDOWS,平台名称:WINDOWS, proxy:Proxy(),setWindowRect:true,strictFileInteractability:false, 超时:{隐式:0,pageLoad:300000,脚本:30000}, unhandledPromptBehavior:关闭并通知, webauthn:virtualAuthenticators:true}会话ID: 6793977a08f3b6eefb43d17e34597557 ***元素信息:位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)上的{Using = id,value = btnLogin} 在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知 来源) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 源)位于java.lang.reflect.Constructor.newInstance(未知源) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) 在 org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) 在 org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323) 在 org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:372) 在org.openqa.selenium.By $ ById.findElement(By.java:188)处 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315) 在src.variablesRepo。(variablesRepo.java:12)... 33 more
解决方法
实际问题发生在您的variableRepo类中。参见下面错误日志的底部
在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315) 在src.variablesRepo。(variablesRepo.java:12)
之所以会出现此问题,是因为在WebDriver Driver = variablesRepo.driver;
类的第NewTest
行为驱动程序创建了两个线程,而在variablesRepo
中使用驱动程序查找登录按钮时又创建了一个线程类。这就是为什么当您尝试直接在NewTest
中找到登录按钮时,它就像您在@ BeforeSuite
方法中打开url所使用的线程一样成功。
如果您只想初始化一次驱动程序并用在所有其他类中,请使用BaseTest类,并实现get_driver类方法来初始化驱动程序。并使用此方法在所有测试类中获取驱动程序。您可以在下面阅读How can I initialize web driver globally?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。