gpt4 book ai didi

java - 转换为 Webdriver 后使用 Selenium Grid 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:02:33 24 4
gpt4 key购买 nike

从 RC 切换到 Selenium Webdriver 后,Selenium Grid 不再工作。请注意,我的大部分测试仍在 RC 中,但一次一点地转换为 Webdriver,因此仍然需要 Selenium 实例。看起来我的驱动程序和/或浏览器(Selenium)实例在并行运行时被覆盖。

这是我的代码:

public class SeleniumTestSupport
{

private static Properties singleSharedProperties;
private static Selenium webmailsingleSharedBrowser;
protected Selenium webmailbrowser;
protected WebDriver singleSharedDriver;

protected WebDriver driver;
protected Selenium browser;//was protected, now public
protected static String domain;
Integer flag = 0;

@BeforeSuite(alwaysRun = true)
public void startSeleniumClient() {
}

@BeforeTest(alwaysRun = true)
public void distributeTests(){
}

@BeforeClass(alwaysRun = true)
public void initBrowser() {
}

@BeforeMethod(alwaysRun = true)
public void logIn() {

singleSharedProperties = new Properties(System.getProperties());
try {
singleSharedProperties.load(getClass().getClassLoader().getResourceAsStream("selenium.properties"));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}

String serverHost = singleSharedProperties.getProperty("selenium.serverHost", "localhost");
String serverPortText = singleSharedProperties.getProperty("selenium.serverPort", "4444");
int serverPort;
try {
serverPort = Integer.parseInt(serverPortText);
} catch (NumberFormatException e) {
throw new RuntimeException("Unable to parse selenium.serverPort '" + serverPortText + "'");
}
String browserStartCommand = singleSharedProperties.getProperty("selenium.browserStartCommand");

System.out.println("serverhost=" + serverHost);
System.out.println("serverport=" + serverPort);
System.out.println("browserStartCommand=" + browserStartCommand);
System.out.println("url=" + singleSharedProperties.getProperty("teamconnect.url"));


DesiredCapabilities capability = DesiredCapabilities.firefox();
singleSharedDriver = new RemoteWebDriver(capability);
driver=singleSharedDriver;
browser = new WebDriverBackedSelenium(singleSharedDriver, singleSharedProperties.getProperty("teamconnect.url"));


String usernamePassword = singleSharedProperties.getProperty("teamconnect.user." + getUserGroup());
String username = StringUtils.substringBefore(usernamePassword, "/");
String password = StringUtils.substringAfter(usernamePassword, "/");



driver.get(singleSharedProperties.getProperty("teamconnect.url"));


//This code is some setup for each Test, basically, logging into the application...
LoginPage loginPage = new LoginPage(browser);
loginPage.setUsername(username);
loginPage.setPassword(password);
loginPage.clickLogIn();
//more code later, removed for brevity


}

这里有一些更多信息:

这里是测试的java代码:

包 XXXXXXXXXXXXX;导入 XXXXXXXXXXXXX;

@Test(groups = { "admin"})公共(public)类 EN1200_SR0050_DesignerRightsTest 扩展了 SeleniumTestSupport{

@Test
public void testAllowAllAndDenyAllGroupDesignerRights2() {

GlobalNavigationPage globalNavigationPage = new GlobalNavigationPage(browser);


// Click Admin tab from global navigation bar.
globalNavigationPage.clickAdminTab();

这是 GlobalNavigationPage java 代码:

包 XXXXXXXXXXXX;

导入XXXXXXXXXXXXXXX;

公共(public)类 GlobalNavigationPage 扩展了 EntityPage{

Reusable_Actions reusable_Actions = new Reusable_Actions();
Page page = new Page(browser);

public GlobalNavigationPage(Selenium browser) {
super(browser);
}


public void clickAdminTab() {

long start = System.currentTimeMillis();

while (System.currentTimeMillis() - start < WAIT_TIME_IN_SECONDS * 2000) {

// if element is present return
if (browser.isElementPresent(LNK_ADMIN)) {
return;
}
// wait for 1/10 of a second
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}}

driver.findElement(By.id(LNK_ADMIN)).click();
browser.setTimeout(Integer.toString(WAIT_TIME_IN_SECONDS * 2000));
browser.waitForPageToLoad(Integer.toString(WAIT_TIME_IN_SECONDS * 2000));
assertTextNotPresent("System Error Has Occured!");
}

最佳答案

我的猜测是您对如何运行测试有自定义配置。

您的集线器配置很可能是这样的:

Only run RC tests.

如果您想运行 WebDriver 测试,则需要指定要使用的 WebDriver 浏览器数量。

{
"capabilities":
[
{
"browserName": "*firefox",
"maxInstances": 5,
"seleniumProtocol": "Selenium"
},
{
"browserName": "*googlechrome",
"maxInstances": 5,
"seleniumProtocol": "Selenium"
},
{
"browserName": "*iexplore",
"maxInstances": 1,
"seleniumProtocol": "Selenium"
},
{
"browserName": "firefox",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
},
{
"browserName": "chrome",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
},
{
"browserName": "internet explorer",
"maxInstances": 1,
"seleniumProtocol": "WebDriver"
}
],
"configuration":
{
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": 5555,
"host": ip,
"register": true,
"registerCycle": 5000,
"hubPort": 4444,
"hubHost": ip
}
}

您可以看到我们可以为 RC 测试指定 "Selenium",为 Selenium 2 测试指定 "WebDriver"

关于java - 转换为 Webdriver 后使用 Selenium Grid 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21889531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com