维护并重用现有的webdriver浏览器实例 – java

基本上每次我从eclipse运行我的java代码时,webdriver都会启动一个新的ie浏览器并在大多数情况下成功执行我的测试。 但是,我有很多测试要运行,webdriver每次启动一个新的浏览器会话都很痛苦。 我需要一种方法来重用以前打开过的浏览器; 所以webdriver会打开,即第一次,然后第二次,我运行我的eclipse程序,我希望它只是拿起以前的浏览器实例并继续在同一个实例上运行我的测试。 这样,我每次运行程序时都不会启动新的浏览器会话。

假设你有100个测试要在eclipse中运行,你点击那个运行按钮然后它们全部运行,然后在大约第87次测试时你得到一个错误。 然后你回到eclipse,修复那个错误,但是你必须从头再次重新运行所有100个测试。

在第87次测试中修复错误,然后从第87次测试恢复执行,而不是从头开始重新执行所有测试,即从测试0一直到100,这将是很好的。希望,我很清楚得到你们的帮助,谢谢顺便说一下。

以下是我尝试维护和重用webdriver Internet Explorer浏览器实例的尝试:

public class demo extends RemoteWebDriver { public static WebDriver driver; public Selenium selenium; public WebDriverWait wait; public String propertyFile; String getSessionId; public demo() { // constructor DesiredCapabilities ieCapabilities = DesiredCapabilities .internetExplorer(); ieCapabilities .setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); driver = new InternetExplorerDriver(ieCapabilities); this.saveSessionIdToSomeStorage(getSessionId); this.startSession(ieCapabilities); driver.manage().window().maximize(); } @Override protected void startSession(Capabilities desiredCapabilities) { String sid = getPreviousSessionIdFromSomeStorage(); if (sid != null) { setSessionId(sid); try { getCurrentUrl(); } catch (WebDriverException e) { // session is not valid sid = null; } } if (sid == null) { super.startSession(desiredCapabilities); saveSessionIdToSomeStorage(getSessionId().toString()); } } private void saveSessionIdToSomeStorage(String session) { session=((RemoteWebDriver) driver).getSessionId().toString(); } private String getPreviousSessionIdFromSomeStorage() { return getSessionId; } } 

我希望这里是通过覆盖来自remoteWebdriver的startSession()方法,它会以某种方式检查我已经在ie中打开了一个webdriver浏览器的实例,它会改为使用该实例,而不是每当我点击时重新创建一个新实例在eclipse中“运行”按钮。

我也可以看到,因为我正在从构造函数创建一个“新的驱动程序实例”,因为构造函数总是先执行,它会自动创建新的驱动程序实例,所以我可能需要以某种方式改变它,但不知道如何。

我是stackoverflow和selenium webdriver的新手,希望有人在这里可以提供帮助。

谢谢!

回答你的问题:

不可以。您无法使用计算机上当前运行的浏览器。 但是,只要它在同一执行中,您就可以使用相同的浏览器进行不同的测试。

但是,听起来你真正的问题是一遍又一遍地运行100次测试。 我建议使用测试框架(如TestNG或JUnit)。 通过这些,您可以指定要运行的测试(TestNG将生成所有失败的测试的XML文件,因此当您运行它时,它将只执行失败的测试)。

实际上你可以再次重复使用同一个会话..

在节点客户端中,您可以使用以下代码附加到现有的selenium会话

 var browser = wd.remote('http://localhost:4444/wd/hub'); browser.attach('df606fdd-f4b7-4651-aaba-fe37a39c86e3', function(err, capabilities) { // The 'capabilities' object as returned by sessionCapabilities if (err) { /* that session doesn't exist */ } else { browser.elementByCss("button.groovy-button", function(err, el) { ... }); } }); ... browser.detach(); 

要获得selenium会话ID,

 driver.getSessionId(); 

注意:这仅在Node Client中可用。要在JAVA或C#中执行相同的操作,您必须覆盖selenium的execute方法以捕获sessionId并将其保存在本地文件中并再次读取它以附加现有的selenium会话

我尝试了以下步骤来使用相同的浏览器实例,它对我有用:

如果您在不同的软件包中使用通用或类1,则以下代码段将起作用 –

 package zgenerics; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.BeforeTest; import org.openqa.selenium.WebDriver; 

//第1类:

 public class Generics { public Generics(){} protected WebDriver driver; @BeforeTest public void maxmen() throws InterruptedException, IOException{ driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); String appURL= "url"; driver.get(appURL); String expectedTitle = "Title"; String actualTitle= driver.getTitle(); if(actualTitle.equals(expectedTitle)){ System.out.println("Verification passed"); } else { System.out.println("Verification failed"); } } 

// 2级:

 package automationScripts; import org.openqa.selenium.By; import org.testng.annotations.*; import zgenerics.Generics; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; public class Login extends Generics { @Test public void Login() throws InterruptedException, Exception { WebDriverWait wait = new WebDriverWait(driver,25); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(""))); driver.findElement(By.cssSelector("")).sendKeys(""); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(""))); driver.findElement(By.xpath("")).sendKeys(""); } } 

如果您的Generics类位于同一个包中,则只需在代码中进行以下更改:

 public class Generics { public Generics(){} WebDriver driver; } 

只需从Webdriver代码行中删除受保护的单词即可。 第1类的剩余代码保持不变。

此致,Mohit Baluja

我通过扩展类(Javainheritance)并创建一个xml文件来尝试它。 我希望以下示例有助于:

第1类:

  package zgenerics; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.BeforeTest; import org.openqa.selenium.WebDriver; public class SetUp { public Generics(){} protected WebDriver driver; @BeforeTest public void maxmen() throws InterruptedException, IOException{ driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); String appURL= "URL"; driver.get(appURL); String expectedTitle = "Title"; String actualTitle= driver.getTitle(); if(actualTitle.equals(expectedTitle)){ System.out.println("Verification passed"); } else { System.out.println("Verification failed"); } } 

第2类:

  package automationScripts; import org.openqa.selenium.By; import org.testng.annotations.Test; import zgenerics.SetUp public class Conditions extends SetUp { @Test public void visible() throws InterruptedException{ Thread.sleep(5000); boolean signINbutton=driver.findElement(By.xpath("xpath")).isEnabled(); System.out.println(signINbutton); boolean SIGNTEXT=driver.findElement(By.xpath("xpath")).isDisplayed(); System.out.println(SIGNTEXT); if (signINbutton==true && SIGNTEXT==true){ System.out.println("Text and button is present"); } else{ System.out.println("Nothing is visible"); } } } 

第3类:

  package automationScripts; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.testng.annotations.Test; public class Footer extends Conditions { @Test public void footerNew () throws InterruptedException{ WebElement aboutUs = driver.findElement(By.cssSelector("CssSelector")); aboutUs.click(); WebElement cancel = driver.findElement(By.xpath("xpath")); cancel.click(); Thread.sleep(1000); WebElement TermsNCond = driver.findElement(By.xpath("xpath")); TermsNCond.click(); } } 

现在使用以下代码创建一个xml文件,并将testng.xml作为testng套件运行:复制并粘贴到代码下面并相应地进行编辑。

        

这将运行三个以上的类。 这意味着一个浏览器和不同的测试。 我们可以通过按照上面的类中的字母顺序设置类名来设置执行顺序。