2017-09-25 151 views
0

我使用了两个类,其中一个是BaseCode,其中声明了所有基本方法。类的定义如下:两个浏​​览器在Selenium中打开

package CodeBase; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class BaseCode { 

    //All the Universal variables for TestBase class are declared here: 
     public static WebDriver driver = null; 

    //All Universal Methods for TestBase class are declared here: 
      public void launchBrowser(String baseUrl) throws Exception{ 
       try { 
         System.out.println("Launching the Chrome Browser"); 
         String driverpath = "E:\\Learning\\Selenium\\Drivers\\ChromeDriver\\chromedriver.exe"; 
         System.setProperty("webdriver.chrome.driver",driverpath); 
         driver = new ChromeDriver(); 
         driver.manage().window().maximize(); 
         System.out.println("Opening URL: " + baseUrl); 
         driver.navigate().to(baseUrl); 
         TimeUnit.SECONDS.sleep(5); 
      }catch(Exception E) { 
       System.out.println(E.getMessage() +"\n" + E.getStackTrace()); 
       } 
      } 
} 

现在我在哪里使用上述launchBrowser()第二类如下:

package projectpack; 

import java.io.File; 
import java.io.IOException; 
import java.util.concurrent.TimeUnit; 
import org.apache.commons.io.FileUtils; 
import org.openqa.selenium.By; 
import org.openqa.selenium.OutputType; 
import org.openqa.selenium.TakesScreenshot; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.testng.annotations.Test; 
import CodeBase.BaseCode; 



public class Flipkart { 

    public static BaseCode B = new BaseCode(); 

    @Test 
    public void testFlipkart() { 
     String url = "https://www.flipkart.com/"; 
     try { 
      B.launchBrowser(url); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     WebDriver driver = new ChromeDriver(); 
     try { 
     TimeUnit.SECONDS.sleep(5); 
     this.takeSnapShot(driver, "E://Learning/Selenium/Screenhots/Flipkart/Intro.jpeg"); 
     String Prod1 = "philips bt120"; 
     WebElement Search = driver.findElement(By.cssSelector("._1WMLwI .LM6RPg")); 
     Search.sendKeys(Prod1); 
     this.takeSnapShot(driver, "E://Learning/Selenium/Screenhots/Flipkart/Step1.jpeg"); 
     Search.submit(); 
     this.takeSnapShot(driver, "E://Learning/Selenium/Screenhots/Flipkart/Step2.jpeg"); 
     WebElement Object = driver.findElement(By.xpath("//*[@id=\"container\"]/div/div[1]/div/div[2]/div/div[2]/div/div[3]/div/div/div[1]/div/a[2]")); 
     Object.click(); 
     this.takeSnapShot(driver, "E://Learning/Selenium/Screenhots/Flipkart/Step3.jpeg"); 
     driver.quit(); 
     }catch(Exception E) { 
      System.out.println(E.getMessage() + "\n" + E.getStackTrace()); 
     } 
    } 
} 

当我运行该代码,它的开口两个浏览器,而不是一个。第一个已经重定向到flipkart.com,但第二个只显示一个空白窗口。控制台输出如下:

[RemoteTestNG] detected TestNG version 6.12.0 
Launching the Chrome Browser 
Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 47688 
Only local connections are allowed. 
Sep 25, 2017 9:58:20 PM org.openqa.selenium.remote.ProtocolHandshake createSession 
INFO: Detected dialect: OSS 
Opening URL: https://www.flipkart.com/ 
Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 47270 
Only local connections are allowed. 
Sep 25, 2017 9:58:34 PM org.openqa.selenium.remote.ProtocolHandshake createSession 
INFO: Detected dialect: OSS 
no such element: Unable to locate element: {"method":"css selector","selector":"._1WMLwI .LM6RPg"} 
    (Session info: chrome=60.0.3112.113) 
    (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information) 
Command duration or timeout: 0 milliseconds 

如果当时留在同一个浏览器,它会发现Search WebElement但其打开第二个浏览器。

任何人都可以帮助如何停止打开两个浏览器。就在尝试捕捉后,一旦在LaunchBrowser方法,一旦

driver = new ChromeDriver(); 

+1

步骤,你会立即看到如何'新的ChromeDriver()'被调用两次。另一个建议,为什么不'Flipkart'扩展'BaseCode'?虽然我通常相信**组合继承**,我认为这是继承更有意义的情况。这也会使跟踪“驱动程序”变得更容易,并且正确使用它,所以你不必将它传递给不同的类。 – mrfreester

+0

只需创建基类的对象,然后在这里调用它 – iamsankalp89

回答

0

你两次调用下面的代码。这就是为什么你有两个浏览器。

0

你在课堂上初始化铬司机2次在一个函数一个

driver = new ChromeDriver(); 
+0

但是如何使用如果我不初始化驱动程序,Flipkart类中的方法应该像这样使用: 'BaseCode.driver.findElement()' 或者有没有其他方法可以使用Flipkart类中的方法 –

0

我有同样的问题,但我发现我有“@BeforeSuite(alwaysRun =真)” 之前所有其他testNG注释。我并没有在代码@afterSuite(它可能通过你的代码忘了一个测试之后将其删除。

取下来它得到解决后。