2

我對Selenium很新。下面給出的是我的第一個測試腳本。雖然啓動ChromeDriver獲得例外

問題:它是否需要硒服務器來測試本地網站(它與運行的測試腳本位於同一臺機器上)。

也嘗試從Internet Explorer執行相同的腳本也仍然相同。 它只是打開瀏覽器並關閉(因爲最後阻止)它。

import org.openqa.selenium.chrome.ChromeDriver; 
public class TestScript { 
    /** 
    * @param args 
    * @throws InterruptedException 
    */ 
    public static void main(String[] args) { 
     ChromeDriver driver = null; 
     try 
     { 
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\user1\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"); 
      driver = new ChromeDriver(); 
      System.out.println("Opening the Browser"); 
      driver.get("http://localhsot:5080/myWebSite/8450191#"); 
      System.out.println("Open the Browser"); 
      System.out.println(""); 
      System.out.println("Title" +driver.getTitle()); 

     } 
     catch (Exception ie) 
     { 
      ie.printStackTrace(); 
     } 
     finally 
     { 
      System.out.println("Quitting the Browser"); 
      driver.close(); 
      driver.quit(); 
     } 
    } 

} 

例外:下面是同時從Eclipse執行我得到異常:

[2820:6204:36503609:ERROR:gpu_info_collector_win.cc(93)] Can't retrieve a valid WinSAT assessment. 
org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12' 
System info: host: '01hw535163', ip: '10.72.15.53', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_17' 
Driver info: driver.version: ChromeDriver 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548) 
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216) 
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:112) 
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:116) 
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:162) 
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:108) 
    at com.seic.scripts.TestScript.main(TestScript.java:16) 
Caused by: org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start. 
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12' 
System info: host: '01hw535163', ip: '10.72.15.53', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_17' 
Driver info: driver.version: ChromeDriver 
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:165) 
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:62) 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527) 
    ... 6 more 
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:37571/status] to be available after 20009 ms 
    at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:104) 
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:163) 
    ... 8 more 
Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException 
    at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:143) 
    at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:79) 
    ... 9 more 
Caused by: java.util.concurrent.TimeoutException 
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:228) 
    at java.util.concurrent.FutureTask.get(FutureTask.java:91) 
    at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130) 
    ... 10 more 
Quitting the Browser 
Exception in thread "Main Thread" java.lang.NullPointerException 
    at com.scripts.TestScript.main(TestScript.java:31) 

Chrome的版本:21.0.1171.0

操作系統:Windows 7 64位。

硒網絡驅動程序版本:2.39.0

+0

你有沒有試過使用Firefox的代碼?您可以刪除System.setProperty()並直接調用driver = new FirefoxDriver()。我也看到你的代碼中本地主機的拼寫錯誤。 – Sighil

回答

3

你得到一個有點困惑,你需要指向硒什麼。

這裏:

System.setProperty("webdriver.chrome.driver", "C:\\Users\\user1\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"); 

此設置,webdriver.chrome.driver是一個設置,硒會讀找出ChromeDriver位於何處,實際 Chrome安裝。

https://code.google.com/p/selenium/wiki/ChromeDriver

您必須下載ChromeDriver和所在應該是你通過什麼爲webdriver.chrome.driver財產。

我想這就是當你嘗試用IE瀏覽器同樣的原因,也要求我猜你沒有一個司機:

https://code.google.com/p/selenium/wiki/InternetExplorerDriver

在你是否需要一個「服務器」方面 - >爲你的測試,不,你沒有遠程運行它們。一旦你決定遠程運行它們(即使用RemoteWebDriver而不是ChromeDriver),那麼你將需要Selenium服務器。

+0

非常感謝您的回答。我已經下載了ChromeDriver。現在我得到了異常。請幫助.. – Naresh

+0

開始ChromeDriver 端口= 2275 版本= 14.0.836.0 \測試\自動化\ proxy_launcher.cc(89):錯誤:價值:app_launched 實際:1 預計:AUTOMATION_SUCCESS 那就是:0 等待瀏覽器進程自動ping時出錯 。\ test \ automation \ proxy_launcher.cc(244):錯誤:值:automation() - > GetBrowserWindowCount(&window_count) 實際:false 預計值:true 。\ test \ automation \ proxy_launcher.cc(244)的值:browser_proxy.get() Actual:false Expected:true – Naresh

+0

。\ test \ automation \ proxy_launcher.cc(269):error:ValueFor:WaitForBrowserProcessToQuit(TestTimeouts :: action_max_timeout_ms(),&exit_code) Actual:false 預期:真 \測試\自動化\ proxy_launcher.cc(270):錯誤:exit_code 實際值::的值-1 預期:0 org.openqa.selenium.WebDriverException:無法要麼發射或連接到瀏覽器。請檢查ChromeDriver是否是最新的。使用Chrome二進制文件:C:\ Users \ Bnaresh \ AppData \ Local \ Google \ Chrome \ Application \ chrome.exe(警告:服務器未提供任何堆棧跟蹤信息) 命令持續時間或超時:51.73秒 – Naresh

0

我會建議,而不是使用System.setProperty()將您的驅動程序放在相同的文件夾中,並指向系統變量路徑中的該文件夾。那麼你根本不需要在代碼中指向它,並且更新你的驅動程序將不需要代碼重寫。