WebDriver Summary-how to launch different browsers
Start Firefox Browser.
1 this situation applies when Firefox is installed in the default path
WebDriver driver = new FirefoxDriver (); / / directly new a FirefoxDriver
Navigation navigation = driver.navigate ()
/ / enter the home page of Baidu
Navigation.to ("http://www.baidu.com");"
2 this situation applies when Firefox is not installed in the default path
System.out.println ("start firefox browser...")
System.setProperty ("webdriver.firefox.bin", / / specify the installation path of firefox
"D:/Program Files/Mozilla Firefox/firefox.exe")
WebDriver driver = new FirefoxDriver ()
Navigation navigation = driver.navigate ()
Navigation.to ("http://www.baidu.com/");"
3 in this case, you can load the plug-in for Firefox.
First of all, we need to know why we need to load plug-ins. The reason is that when webdriver starts the browser, it is a clean browser without tasks, plug-ins, and cookies information (even if your native firefox installs some plug-ins, webdriver startup firefox does not have these plug-ins), but it is possible that the system under test needs plug-ins or debugging, and so on. At this point, you can load plug-ins when starting firefox using the following example: load firebug plug-ins
Import java.io.File
Import java.io.IOException
Import org.openqa.selenium.Alert
Import org.openqa.selenium.WebDriver
Import org.openqa.selenium.WebDriver.Navigation
Import org.openqa.selenium.firefox.FirefoxDriver
Import org.openqa.selenium.firefox.FirefoxProfile
Public class TestDemo {
Public static void main (String [] args) {
/ / TODO Auto-generated method stub
System.out.println ("start firefox browser...")
System.setProperty ("webdriver.firefox.bin"
"C:/Program Files (x86) / Mozilla Firefox/firefox.exe")
File file = new File ("/ files/firebug-2.0.7-fx.xpi")
FirefoxProfile profile = new FirefoxProfile ()
Try {
Profile.addExtension (file)
} catch (IOException e) {
E.printStackTrace ()
}
Profile.setPreference ("extensions.firebug.currentVersion", "2.0.7")
/ / active firebug extensions
Profile.setPreference ("extensions.firebug.allPagesActivation", "on")
WebDriver driver = new FirefoxDriver (profile)
Driver.get ("http://www.baidu.com");"
System.out.println ("start firefox browser succeed...")
}
}
-
The above code is not called to notify the following exception
Start firefox browser...
Exception in thread "main" org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary (C:\ Program Files (x86)\ Mozilla Firefox\ firefox.exe) on port 7055; process output follows:
Null
Build info: version: '2.53.0mm, revision:' 35ae25bund, time: '2016-03-15 16V 5740'
System info: host: 'XL-20150414QGDQ', ip:' 192.168.80.6, os.name: 'Windows 7, os.arch:' amd64', os.version: '6.1, java.version:' 1.7.080'
Driver info: driver.version: FirefoxDriver
At org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start (NewProfileExtensionConnection.java:128)
At org.openqa.selenium.firefox.FirefoxDriver.startClient (FirefoxDriver.java:271)
At org.openqa.selenium.remote.RemoteWebDriver. (RemoteWebDriver.java:119)
At org.openqa.selenium.firefox.FirefoxDriver. (FirefoxDriver.java:218)
At org.openqa.selenium.firefox.FirefoxDriver. (FirefoxDriver.java:211)
At org.openqa.selenium.firefox.FirefoxDriver. (FirefoxDriver.java:207)
At org.openqa.selenium.firefox.FirefoxDriver. (FirefoxDriver.java:124)
At TestDemo.main (TestDemo.java:27)
Caused by: org.openqa.selenium.firefox.UnableToCreateProfileException: java.io.FileNotFoundException:\ files\ firebug-2.0.7-fx.xpi (the system cannot find the specified path.)
Build info: version: '2.53.0mm, revision:' 35ae25bund, time: '2016-03-15 16V 5740'
System info: host: 'XL-20150414QGDQ', ip:' 192.168.80.6, os.name: 'Windows 7, os.arch:' amd64', os.version: '6.1, java.version:' 1.7.080'
Driver info: driver.version: FirefoxDriver
At org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk (FirefoxProfile.java:427)
At org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start (NewProfileExtensionConnection.java:85)
... 7 more
Caused by: java.io.FileNotFoundException:\ files\ firebug-2.0.7-fx.xpi (the system cannot find the specified path.)
At java.io.FileInputStream.open (Native Method)
At java.io.FileInputStream. (Unknown Source)
At org.openqa.selenium.firefox.internal.FileExtension.obtainRootDirectory (FileExtension.java:80)
At org.openqa.selenium.firefox.internal.FileExtension.writeTo (FileExtension.java:59)
At org.openqa.selenium.firefox.FirefoxProfile.installExtensions (FirefoxProfile.java:443)
At org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk (FirefoxProfile.java:421)
... 8 more
(4) in the case of (3), it is not transferred.
For each startup, if it is troublesome to configure profile in the code as above, you can use the following method to start the configuration of the local machine's firefox. In other words, we can configure the local firefox in advance and start it with webdriver so that any plug-in installed by the local firefox can be used directly without the need to configure profile:
Public static void main (String [] args) {
/ / TODO Auto-generated method stub
System.out.println ("start firefox browser...")
System.setProperty ("webdriver.firefox.bin", "C:/Program Files (x86) / Mozilla Firefox/firefox.exe")
ProfilesIni pi = new ProfilesIni ()
FirefoxProfile profile = pi.getProfile ("default")
WebDriver driver = new FirefoxDriver (profile)
Driver.get ("http://www.baidu.com");"
System.out.println ("start firefox browser succeed...")
}
Start IE Browser.
PS: all browsers except Firefox need to download their own Driver from Selenium's official website http://docs.seleniumhq.org/download/.
1 start the local IE Browser.
System.setProperty ("webdriver.ie.driver"
"E:\\ selenium\\ IEDriverServer.exe"); / / the local path where IEDriverServer.exe is located
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer ()
IeCapabilities.setCapability (InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true)
WebDriver driver = new InternetExplorerDriver (ieCapabilities)
Driver.get ("http://www.baidu.com");"
Start Chrome Browser.
1 start the local Chrome Browser.
Public static void main (String [] args) {
/ / TODO Auto-generated method stub
System.setProperty ("webdriver.chrome.driver"
"E:\\ selenium\\ chromedriver.exe"); / / the local path where chromedriver.exe is located
WebDriver driver = new ChromeDriver ()
Driver.get ("http://www.baidu.com");"
Driver.findElement (By.id ("kw")) .sendKeys (Keys.chord (Keys.SHIFT, "webdriver"))
Driver.findElement (By.id (su)) .click ()
Driver.close ()
}