Webdriver Getting Started-java

Source: Internet
Author: User

How to open a browser with webdriver, we commonly used browsers have Firefox and IE two kinds, Firefox is selenium support more mature browser, many new features will be reflected in Firefox. But do the page test, startup speed is relatively slow, start after the speed is acceptable.

Start the Firefox browser

Create a new Firefoxdriver
If Firefox is not installed by default on the C drive, you need to set its path




Start IE browser

Create a newinstance of the Internet Explorer driver
Webdriver Driver = Newinternetexplorerdriver ();

Launch Htmlunit Browser

Createa new instance of the Htmlunit driver
Webdriverdriver = new Htmlunitdriver ();

Start the Chrome browser

Download ChromeDriver.exe please click here

Createa new instance of the Chromedriver

System.setproperty ("Webdriver.chrome.driver", Bspath);
Webdriverdriver = new Chromedriver ();

To test a Web page, first open the address of the page being tested (for example:http://www.baidu.com), and the Get method provided by web driver can open a page:
Thedriver to visit Google
Driver.get ("http://www.baidu.com");
You can also call the following methods
Driver.navigate (). to ("http://www.baidu.com");
The test script is as follows
Package Com.test.ui.demo;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.firefox.FirefoxDriver;
Import org.openqa.selenium.support.ui.ExpectedCondition;
Import org.openqa.selenium.support.ui.WebDriverWait;
Import Org.testng.annotations.AfterMethod;
Import Org.testng.annotations.BeforeMethod;
Import Org.testng.annotations.Test;

public class Testwebdriver {
Private Webdriver driver = null;
Private String URL = "Http://www.baidu.com";
This method is executed before each use case executes
@BeforeMethod
public void StartUp () {
If Firefox is not installed on the C drive you need to do the following sentence, otherwise please comment out
System.setproperty ("Webdriver.firefox.bin", "D:/program Files/mozilla firefox/firefox.exe");
Driver = new Firefoxdriver ();
}
This method is executed after each use case executes
@AfterMethod
public void TearDown () {
Exit operation
Driver.quit ();
Driver = null;
}
@Test
public void Starttest () {
Open a new window
Driver.get (URL);
}
}

Various browser Comparisons ↓

Webdirver support for browsers htmlunit driver Advantages: Htmlunit driver does not actually open the browser, running fast. For automated test cases that test with browsers such as Firefox, the speed is usually slow, and htmlunit driver is undoubtedly a good solution to this problem.
Cons: It's not good enough for JavaScript, and when there are complex JavaScript on the page, it's often not captured by the page element.
Use:
Webdriver Driver = new Htmlunitdriver ();

Firefox driver advantages: Firefox dirver to the page of automated testing support is relatively good, very intuitive to simulate the operation of the page, JavaScript support is also very perfect, basically all the actions on the page Firefox driver can be simulated.
Disadvantage: Start very slow, running is also relatively slow, however, after the start of webdriver operation speed although unpleasant but still acceptable, it is recommended not to stop frequent Firefox Driver.
Use:
Webdriver Driver = new Firefoxdriver ();
The property values of Firefox profile can be changed, for example, we may need to use the agent to surf the internet, can be modified as follows:
Firefoxprofile profile = new Firefoxprofile ();

Using profile

Profilesini allprofiles = new Profilesini ();
Firefoxprofile profile = Allprofiles.getprofile ("Default");
Driver = new Firefoxdriver (profile);

Using proxies

Profile.setpreference ("Network.proxy.type", 1);

HTTP protocol proxy Configuration

Profile.setpreference ("Network.proxy.http", Proxyip);
Profile.setpreference ("Network.proxy.http_port", ProxyPort);

All protocols common one proxy configuration, if configured individually, this setting is false, again similar to the configuration of HTTP

Profile.setpreference ("Network.proxy.share_proxy_settings", true);

For localhost's no proxy, it must be configured here, otherwise it will not be able to communicate with Webdriver

Profile.setpreference ("network.proxy.no_proxies_on", "localhost");

Start Firefox as an agent

Firefoxdriver ff = new Firefoxdriver (profile);

InternetExplorer Driver Advantages: Intuitively simulates the user's actual operation and provides perfect support for JavaScript.
Cons: It is the slowest in all browsers and can only be run under Windows, and the support for CSS and XPath is not good enough.
Use:
Webdriver Driver = new Internetexplorerdriver ();

Element Action ↓ Find element

The Findelement method that uses how to find the page element Webdriver can be used to find an element of the page, and the most common method is to find it with the ID and name. Here are a few more commonly used methods.
The by ID assumes that the page is written like this:
<input type= "text" name= "UserName" id= "user"/>
Then you can find the elements of the page in this way:
Find by ID:
webelement element = Driver.findelement (by.id ("user"));
By name or by name:
webelement element = Driver.findelement (By.name ("UserName"));
By XPath or lookup by XPath:
webelement element =driver.findelement (By.xpath ("//input[@id = ' user ')");
The by Class name assumes that the page is written like this:

<div class= "Top" ><span>head</span></div><divclass= "Top" ><span>headname</ Span></div>
You can find page elements by doing this:
list<webelement>top= driver.findelements (By.classname ("Top"));

By Link text assumes that the page element is written like this:
<a href= "http://www.baidu.com" >baidu</a>>
Then you can find it by:
Webelement baidu=driver.findelement (By.linktext ("Baidu"));

Input Box Pass Value

The Input box (text field or textarea) finds the input box element:
webelement element = Driver.findelement (By.id ("Passwd-id"));
Enter the contents in the Input box:
Element.sendkeys ("test");
Empty the input box:
Element.clear ();
Get the text content of the input box:
Element.gettext ();

Drop-down menu

Drop-Down selection box (select) to find the elements of the drop-down selection box:
Select select = New Select (Driver.findelement (By.id ("select"));
Select the corresponding selection: Select.selectbyvisibletext ("testname");
Or
Select.selectbyvalue ("name");
Do not select the corresponding selection:
Select.deselectall ();
Select.deselectbyvalue ("name");
Select.deselectbyvisibletext ("name");
Or get the value of the selection:
Select.getallselectedoptions ();
Select.getfirstselectedoption ();

Radio Box

Single option (Radio Button) to find the Radio box element:
Webelement sex=driver.findelement (by.id ("Sex"));

Select a single option:

Sex.click ();
Clear a single option:
Sex.clear ();

Determine if a single option has been selected:

Sex.isselected ();

check box

The Multi-option (CheckBox) Multi-option operation is the same as a single radio:
Webelement Area =driver.findelement (by.id ("area."));
Area. Click ();
Area. Clear ();
Area. isSelected ();
Area. IsEnabled ();

Button

button to find the button element:
Webelement Savebutton = driver.findelement (By.id ("Save"));

Click the button:

Savebutton.click ();

Determine if the button is enable:

Savebutton.isenabled ();

The left and right selection boxes, which are available for selection, are moved to the box on the other side and vice versa. For example:

Select Name= New Select (Driver.findelement (by.id ("name"));
Name.selectbyvisibletext ("Hellen");
Webelement addname=driver.findelement (by.id ("AddButton"));
Addname.click ();

Pop-up box

Popup dialog box (popup dialogs) Alert alert = Driver.switchto (). alert ();
Alert.accept ();
Alert.dismiss ();
Alert.gettext ();

Form submission

The actions of the elements in the Form form (form) and other element operations, the submission of the form after the element operation is complete can:
Webelement sub= driver.findelement (by.id ("sub"));
Sub.click ();
Or
Sub.submit ();//only suitable for form submission

Uploading attachments

Upload files (Upload file) to upload file elements operation:
Webelement picfile = driver.findelement (By.id ("Picfile"));
String FilePath = "D:\\report\\600x600x0.jpg";
Picfile. SendKeys (FilePath);

Multi-Window switching

Switching between Windows or frames

First switch to the default frame
Driver.switchto (). Defaultcontent ();
Switch to a frame:
Driver.switchto (). FRAME ("Leftframe");
Switch from one frame to another frame:
Driver.switchto (). FRAME ("MainFrame");
Switch to a window:
Driver.switchto (). Window ("Windowname");

Navigation

Navigation (Navigationand history) opens a new page:
Driver.navigate (). to ("http://www.baidu.com");

Return to the original page via historical navigation:
Driver.navigate (). Forward ();
Driver.navigate (). back ();

The above is a brief introduction of the Webdriver often encountered in the operation, there is a problem to consult the official API documentation

Webdriver Getting Started-java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.