2017-09-25 159 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(它可能通過你的代碼忘了一個測試之後將其刪除。

取下來它得到解決後。