Recently, the test department of the company gave a lecture about how to do a good job of Web automation testing. I did some development work, but I didn't know much about how testers work. A test tool in the lecture is not bad, so I learned it deeply. The content is as follows:
1. Versions of Selenium
Selenium currently has two versions: Selenium-core and selenium-RC.
Selenium-core uses HTML to write test scripts. You can also use selenium-ide to record the scripts.
Only the Firefox version is supported.
Selenium-RC is short for selenium-remote control and is a test class written in a specific language.
Selenium-RC supports many languages. Here we focus on the Java method. This is mainly about selenium-RC.
Method
2. Some preparations
1, of course is to download selenium, to the http://www.openqa.org/selenium/ to download it, remember to select the version of selenium-RC.
2. Learn about XPath. Post to everyone a tutorial http://blog.csdn.net/terryzero/archive/2009/09/06/4523834.aspx
You must learn this. Otherwise, you cannot understand the following content!
3. Install jdk1.5
Iii. Selenium-RC usage
In the selenium-remote-control-0.9.0/Server Directory, we run the Java-jar selenium-server.jar
Then you will see some startup information. To use selenium-RC, it is necessary to start this server.
Of course, there are many parameters at startup. You can refer to the tutorials for these usage on the website, but it is sufficient to add parameters without them.
After selenium server is started, we can start writing test classes!
First, we have a concept that selenium imitates browser behavior. When you run the test class, you will find that selenium will open
Then, the Browser executes your operations.
Okay, first generate our test class:
Public class TestPage2 extends TestCase {<br/> private Selenium selenium; </p> <p> protected void setUp () throws Exception {<br/> String url = "http://xxx.xxx.xxx.xxx/yyy#; <br/> selenium = new DefaultSelenium (" localhost ", SeleniumServer. getDefaultPort <br/> (), "* iexplore", url); <br/> selenium. start (); </p> <p> super. setUp (); </p> <p >}</p> <p> protected void tearDown () throws Exception {</p> <p> selenium. stop (); <br/> super. tearDown (); </p> <p >}< br/>
The code is very simple and serves to initialize a selenium object. Where:
URL: the website you want to test
Localhost: it may not be localhost, but it must be the address started by selenium server.
* Iexplore: It can be another browser type and can be viewed on the website.
Next I will talk about how to use the selenium object for testing.
1. Test text input box
Suppose there is a text input box on the page. The content we want to test is to enter some content in it, and then click a button to see if the page is redirected.
To the required page.
Public void test1 () {</p> <p> selenium. open ("http://xxx.xxx.xxx/yyy"); </p> <p> selenium. type ("xpath = // input [@ name = 'userid']", "test-user"); <br/> selenium. click ("xpath = // input [@ type = 'button ']"); <br/> selenium. waitForPageToLoad ("2000"); <br/> assertEquals (selenium. getTitle (), "Welcome"); <br/>}< br/>
The above code indicates:
1. Call the selenium. Open method. The browser will open the corresponding page
2. Use the type method to input text to the input box.
3. Wait for the page to load
4. Check whether the new page title is what we want.
The selenium script firefox plug-in selenium IDE is used to generate recording. If you test IE, you can use firefox to record and use it after slight changes.
2. Test the drop-down list
Public void test1 () {</p> <p> selenium. open ("http://xxx.xxx.xxx/yyy"); </p> <p> selenium. select ("xpath = // SELECT [@ name = 'sbbusyo ']", "index = 1"); <br/> selenium. click ("xpath = // input [@ type = 'button ']"); <br/> selenium. waitForPageToLoad ("2000"); <br/> assertEquals (selenium. getTitle (), "Welcome"); <br/>}< br/>
We can see that we can use the select method to determine which option to select in the drop-down box.
The select method has many usage options. For more information, see the document.
3. test check box
Public void test1 () {</P> <p> selenium. open ("http://xxx.xxx.xxx/yyy"); </P> <p> selenium. check ("XPath = // input [@ name = 'meick _ 000']"); <br/> selenium. click ("XPath = // input [@ type = 'button ']"); <br/> selenium. waitforpagetoload ("2000"); <br/> assertequals (selenium. gettitle (), "welcome"); <br/>}< br/>
We can use the check method to determine which radio button to choose.
4. Get the text in the text box.
AssertEquals (selenium. getValue ("xpath = // input [@ name = 'wno']"), "1 ");
The getValue method is used to get the value in the text box. It is not the getText method. If it is used incorrectly, it will be depressing.
5. determine whether an element exists on the page
AssertTrue (selenium. isElementPresent ("xpath = // input [@ name = 'meick _ 000']");
This is generally used to test whether something on the page will not be displayed after some data is deleted.
6. Determine which option is selected in the drop-down list.
AssertEquals (selenium. getSelectedIndex ("xpath = // SELECT [@ name = 'hatiming']"), "1 ");
This can be used to determine whether the options displayed in the drop-down list are the expected ones.
7. What if an alert pop-up dialog box exists?
This problem takes a long time. You can close the pop-up cross-box:
If (selenium. isAlertPresent ()){
Selenium. getAlert ();
}
When selenium. getAlert () is called, the alert pop-up dialog box is closed.
You can also use System. out. println (selenium. getAlert () to view the information displayed on the Cross-frame.
During the test, some people will display a lot of alert to view the runtime data, so we can use the following method to close those alert:
While (selenium. isAlertPresent ()){
Selenium. getAlert ();
}
8. How do I test the display of some error messages?
AssertTrue (selenium. getBodyText (). indexOf ("error message")> = 0 );
Remember: When getBodyText is returned, the text on the browser page does not contain html code. To display html code, use the following:
System. out. println (selenium. getHtmlSource ());
These are the most common methods, such as click, type, and getValue.
There is also the need to learn xpath. In fact, xpath can also have "and, or, not" operations:
Selenium. check ("xpath = // input [(@ name = 'knykbn ') and (@ value = 'y')]");
The above content is partially reproduced and javaeye