在本文中,我们将详细介绍硒和并行JUnit-WebDriver实例的各个方面,并为您提供关于各种硒的区别的相关解答,同时,我们也将为您带来关于Java中的静态Webdriver实例同步、JUnit单元
在本文中,我们将详细介绍硒和并行JUnit-WebDriver实例的各个方面,并为您提供关于各种硒的区别的相关解答,同时,我们也将为您带来关于Java中的静态Webdriver实例同步、JUnit单元测试框架与Selenium WebDriver的集成、org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder的实例源码、org.springframework.test.web.servlet.htmlunit.webdriver.WebConnectionHtmlUnitDriver的实例源码的有用知识。
本文目录一览:- 硒和并行JUnit-WebDriver实例(各种硒的区别)
- Java中的静态Webdriver实例同步
- JUnit单元测试框架与Selenium WebDriver的集成
- org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder的实例源码
- org.springframework.test.web.servlet.htmlunit.webdriver.WebConnectionHtmlUnitDriver的实例源码
硒和并行JUnit-WebDriver实例(各种硒的区别)
设置
因此,基本上,我正在尝试实现使用JUnit并行运行的Selenium测试。
为此,我找到了这个JUnit运行器。它真的很好,我非常喜欢。
但是,我在处理WebDriver实例时遇到问题。
我想要的是
在@Test
执行方法之前,应为每个类创建一次每个WebDriver元素。
逻辑上,我可以为此使用类构造函数。实际上,这确实是我测试的要求,因为我需要利用,@Parameters
以便可以相应地创建WebDriver实例(Chrome,FF,IE
…)。
问题
问题是我希望在 类* 完成后而不是在 每种 方法
完成后清除(driver.quit()
)WebDriver实例。但是我不能使用,因为我不能使WebDriver成为静态成员,因为每个类实例必须使用自己的实例(否则测试将尝试在同一浏览器中运行)。
@Test
*@AfterClass
可能的解决方案
我发现了一个可能的建议在这里通过 Mrunal Gosar
。按照他的建议,我将WebDriver改为了ADriver static
ThreadLocal<WebDriver>
,然后我在每个构造函数中使用创建它的实例
// in the classes constructor
driver = new ThreadLocal<WebDriver>() {
@Override
protected WebDriver initialValue() {
return new FirefoxDriver(); /
}
};
针说我用代码替换了每个driver.whatever
调用driver.get().whatever
。
现在,为了解决此问题的最终目的,我还编写了一个@AfterClass
方法,该方法将driver.get().quit();
被编译器接受,因为该变量是静态的。
但是,对此进行测试会导致意外行为。我有一个Selenium
Grid设置,其中有2个节点在远程计算机上运行。我曾按预期运行此设置,但是现在浏览器到处都是垃圾邮件,并且测试失败。(虽然应该运行2个浏览器,但要打开8个以上的浏览器)
我链接的建议该解决方案的线程有人评论说,如果已经使用了像JUnit这样的框架,手动处理线程可能是个坏主意。
我的问题
什么是正确的设计来做到这一点?
我只能想到
- 使这里建议的工作
- 编写一个执行所有其他方法的带@Test注释的方法,然后使用@After实现与@AfterClass相同的效果
- 将构造函数参数保存在成员变量中,并处理以下事实:我必须在
@Test
执行每个带注释的方法之前创建一个浏览器(@Before
用于创建WebDriver实例并@After
关闭会话)
我不太确定选项3是否会遇到可能的问题。如果我在每种方法后都关闭了会话,那么在该节点完成之前的会话之前,Grid-
Server可能实际上在该节点上打开了一个具有全新类的新会话。尽管测试彼此独立,但我仍然觉得这是潜在的危险。
在座的任何人都在积极使用多线程Selenium测试服并且可以指导我什么是正确的设计吗?
Java中的静态Webdriver实例同步
GlobalVariables类包含在我的框架中使用的不同变量,其中之一是WebDriver实例:
public class GlobalVariables
{
public static WebDriver driver;
//Some other static global variables required across my framework
public GlobalVariables(String propertiesFile)
{
initializeVariables(propertiesFile);
}
public void initializeVariables(String propertiesFile)
{
GlobalInitializer obj=new GlobalInitializer();
obj.initialize(String propertiesFile);
}
}
GlobalInitializer包含用于初始化所有GlobalVariable的方法:
public class GlobalInitializer extends GlobalVariables
{
public void initialize(String propertiesFile)
{
//Some logic to read properties file and based on the properties set in it,call other initialization methods to set the global variables.
}
public void initializeDriverInstance(String Browser)
{
driver=new FireFoxDriver();
}
//一些其他方法来初始化其他全局变量。}
我有许多GetElement类,它们使用驱动程序实例获取UI控件元素,例如:
public class GetLabelElement extends GlobaleVariables
{
public static WebElement getLabel(String someID)
{
return driver.findElement(By.id(someId));
}
//Similar methods to get other types of label elements.
}
public class GetTextBoxElement extends GlobaleVariables
{
public static WebElement getTextBox(String someXpath)
{
return driver.findElement(By.xpath(someXpath));
}
//Similar methods to get other types of text box elements.
}
我还有其他类在UI控件上执行一些操作(此类也使用全局变量),例如:
public class GetLabelProperties extends GlobalVariables
{
public static String getLabelText(WebElement element)
{
return element.getText();
}
}
public class PerformAction extends GlobalVariables
{
public static void setText(String textBoxName,String someText)
{
driver.findElement(someLocator(textBoxName)).setText("someText");
}
//Some other methods which may or may not use the global variables to perform some action
}
我在testng中的测试类如下所示:
public class TestClass
{
GlobalVariables globalObj=new GlobalVariables(String propertiesFile);
@Test(priority=0)
{
GlobalVariables.driver.get(someURL);
//Some assertion.
}
@Test(priority=1)
{
WebElement element=GetLabelElement.getLabel(someID);
String labelName=GetLabelProperties.getLabelText(element);
//Some assertion.
}
@Test(priority=2)
{
WebElement element=GetTextBoxElement.getTextBox(someXpath);
PerformAction.setText(element.getText(),someText);
//Some assertion.
}
}
根据场景,我有多个类似的测试类。现在,如果我单独运行它们,则此测试运行良好。但是,当我尝试并行运行它们时,则该测试以某种奇怪的方式失败了。通过分析,我发现每个测试都初始化了它的静态全局变量,从而使其他测试失败了。现在,我应该如何实现我的目标,以最小的框架设计更改并行运行多个测试?我尝试搜索选项,并且遇到了一些选项,即1)使用同步。2)创建ThreadLocal实例(注意:我已经尝试过此解决方案,但仍然是同样的问题。测试相互混合会导致失败。我已将WebDriver实例标记为ThreadLocal,并重写了ThreadLocal的initialValue方法来初始化驱动程序实例。我仍然不确定我是否正确实施了它。)。现在,我不确定在给定场景下如何最好地实现此解决方案中的任何一个。任何帮助表示赞赏。TIA!
JUnit单元测试框架与Selenium WebDriver的集成
junit 和 selenium
JUnit 单元测试框架与 Selenium WebDriver 的集成
简介
JUnit 是一个广泛用于 Java 应用程序单元测试的框架。Selenium WebDriver 是一个用于自动化 Web 应用程序测试的流行工具。将这两者集成在一起,可以轻松地为您的 Web 应用程序测试编写可靠、可维护的单元测试。
集成 JUnit 和 Selenium WebDriver
要集成 JUnit 和 Selenium WebDriver,您需要在项目中添加以下依赖项:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency>
实战示例
以下是一个实战示例,展示如何使用 JUnit 和 Selenium WebDriver 测试 Web 应用程序:
import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class SeleniumJUnitExample { private static WebDriver driver; // BeforeClass: 对所有测试方法执行一次 @BeforeClass public static void setUp() { // 设置驱动程序路径,替换为自己系统中的路径 System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); driver = new ChromeDriver(); driver.manage().window().maximize(); } // AfterClass: 在所有测试方法执行后执行一次 @AfterClass public static void tearDown() { driver.quit(); } @Test public void testLogin() { driver.get("https://www.example.com"); // 定位登录链接并点击 WebElement loginLink = driver.findElement(By.linkText("Login")); loginLink.click(); // 输入用户名和密码并提交 WebElement usernameInput = driver.findElement(By.name("username")); usernameInput.sendKeys("admin"); WebElement passwordInput = driver.findElement(By.name("password")); passwordInput.sendKeys("password"); WebElement loginButton = driver.findElement(By.id("login-button")); loginButton.click(); // 验证是否成功登录 WebElement loggedInText = driver.findElement(By.xpath("//h1[contains(text(), ''Welcome, admin'')]")); assertTrue(loggedInText.isDisplayed()); } }
运行测试
要运行测试,您可以使用如下命令:
mvn test
结论
集成 JUnit 和 Selenium WebDriver 可以显著提升您 Web 应用程序测试的效率和可靠性。这个示例提供了集成和使用这些工具的逐步指南,帮助您轻松自动化测试任务。
以上就是JUnit单元测试框架与Selenium WebDriver的集成的详细内容,更多请关注php中文网其它相关文章!
org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder的实例源码
@Before public void setup() { webClient = mockmvcWebClientBuilder // demonstrates applying a mockmvcConfigurer (Spring Security) // .webAppContextSetup(context,springSecurity()) .webAppContextSetup(context) // for illustration only - defaults to "" .contextpath("") // By default mockmvc is used for localhost only; // the following will use mockmvc for example.com and example.org as well .usemockmvcForHosts("example.com","example.org") .build(); driver = mockmvcHtmlUnitDriverBuilder .webAppContextSetup(context) .build(); /*mockmvc mockmvc = mockmvcBuilders .webAppContextSetup(context) // .apply(springSecurity()) .build(); webClient = mockmvcWebClientBuilder .mockmvcSetup(mockmvc) // for illustration only - defaults to "" .contextpath("") // By default mockmvc is used for localhost only; // the following will use mockmvc for example.com and example.org as well .usemockmvcForHosts("example.com","example.org") .build();*/ }
@Bean @ConditionalOnMissingBean({ WebDriver.class,mockmvcHtmlUnitDriverBuilder.class }) @ConditionalOnBean(mockmvc.class) public mockmvcHtmlUnitDriverBuilder mockmvcHtmlUnitDriverBuilder(mockmvc mockmvc) { return mockmvcHtmlUnitDriverBuilder.mockmvcSetup(mockmvc) .withDelegate(new LocalHostWebConnectionHtmlUnitDriver(this.environment,browserVersion.CHROME)); }
@WithSigningUser @Test public void methodSecurity() { WebDriver driver = mockmvcHtmlUnitDriverBuilder.webAppContextSetup(wac).build(); assertthatThrownBy(() -> { AdminLinkclapage.to(driver); }).hasRootCauseExactlyInstanceOf(AccessDeniedException.class); }
@Bean @ConditionalOnMissingBean({ WebDriver.class,browserVersion.CHROME)); }
@Before @Override public void setup() { super.setup(); webDriver = mockmvcHtmlUnitDriverBuilder .mockmvcSetup(mockmvc) .contextpath(contextpath) .build(); LoginPage loginPage = new LoginPage(webDriver,contextpath,port); loginPage.login(ADMIN_USER_NAME,ADMIN_PASSWORD); }
@Before public void setUp() throws Exception { driver = mockmvcHtmlUnitDriverBuilder .webAppContextSetup(context) .withDelegate(new WebConnectionHtmlUnitDriver(browserVersion.CHROME)) .contextpath("") .build(); }
@Bean @ConditionalOnMissingBean(WebDriver.class) @ConditionalOnBean(mockmvcHtmlUnitDriverBuilder.class) public HtmlUnitDriver htmlUnitDriver(mockmvcHtmlUnitDriverBuilder builder) { return builder.build(); }
@Bean @ConditionalOnMissingBean(WebDriver.class) @ConditionalOnBean(mockmvcHtmlUnitDriverBuilder.class) public HtmlUnitDriver htmlUnitDriver(mockmvcHtmlUnitDriverBuilder builder) { return builder.build(); }
@Before public void setup() { driver = mockmvcHtmlUnitDriverBuilder.webAppContextSetup(context).contextpath("/dddsample").build(); }
@Before public void setup() { this.driver = mockmvcHtmlUnitDriverBuilder.mockmvcSetup(this.mockmvc).build(); }
@Before public void setup() { this.driver = mockmvcHtmlUnitDriverBuilder.mockmvcSetup(this.mockmvc).build(); }
@Before public void setup() { this.driver = mockmvcHtmlUnitDriverBuilder.mockmvcSetup(this.mockmvc).build(); }
/** * Sets up the HTMLUnit Web Client. * * @throws Exception * in case of error during setup. */ @Before public void setupWebClient() throws Exception { webClient = mockmvcWebClientBuilder.webAppContextSetup(wac).build(); webDriver = mockmvcHtmlUnitDriverBuilder.webAppContextSetup(wac).build(); }
org.springframework.test.web.servlet.htmlunit.webdriver.WebConnectionHtmlUnitDriver的实例源码
@Before public void setUp() throws Exception { driver = mockmvcHtmlUnitDriverBuilder .webAppContextSetup(context) .withDelegate(new WebConnectionHtmlUnitDriver(browserVersion.CHROME)) .contextpath("") .build(); }
关于硒和并行JUnit-WebDriver实例和各种硒的区别的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Java中的静态Webdriver实例同步、JUnit单元测试框架与Selenium WebDriver的集成、org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder的实例源码、org.springframework.test.web.servlet.htmlunit.webdriver.WebConnectionHtmlUnitDriver的实例源码等相关内容,可以在本站寻找。
本文标签: