SELENIUM2 Learning -003-selenium2 WebUI Automation Practical example-001-100 degree search

Source: Internet
Author: User

This article mainly through the Baidu search function, carries on the Selenium2 actual combat example to explain, the article appended the source code in 2015-01-16 02:01 the pro-test passes, please the kiss to read. Hope to be able to beginner Selenium2 UI Automation test programming of the pro-helpful. If there is any deficiency, please ask the great God to correct me, I appreciate it!

The script implementation function steps are as follows:

    1. Start the Chrome browser
    2. Open Baidu Website: www.baidu.com
    3. Enter search term: Van Fongping Blog Park
    4. Gets the first item of the search result and opens
    5. Close the Chrome browser (this item has been commented for the show Open effect)

The night has been deep, nonsense less, directly on the source code. Selenium UI Automation Functional test Script: Baidu Search source code is as follows:

1 /**2 * AARON.FFP Inc.3 * Copyright (c) 2014-2015 All rights Reserved.4  */5  PackageMain.java.aaron.selenium;6 7 ImportJava.util.concurrent.TimeUnit;8 9 Importorg.openqa.selenium.By;Ten ImportOrg.openqa.selenium.WebDriver; One Importorg.openqa.selenium.WebElement; A ImportOrg.openqa.selenium.chrome.ChromeDriver; -  - /** the * UI Automation function test script: Baidu search -  *  - * The implementation of the Baidu search function steps are described as follows: - * 1. Start the Chrome browser + * 2. Open Baidu website: www.baidu.com - * 3. Enter search term: Van Fongping Blog Park + * 4. Get the first item of search results and open A * 5. Close the Chrome browser (this item has been commented for the show Open effect) at  * @authorAARON.FFP -  * @version$Id: Baidusearch.java, v 0.1 January 16, 2015 AM 1:10:08 AARON.FFP EXP $ -  */ -  Public classBaidusearch { -     Private Staticwebdriver cd; -     Private StaticString BaseUrl;//Baidu Home Site in     Private StaticWebelement Txt_search;//Search Entry Box -     Private StaticWebelement Btn_search;//Search Button to     Private StaticWebelement Lnk_first;//First Search Results +     Private StaticString searchcontent;//Search Content -      the     /** * * Test main entrance $      * @paramargsPanax Notoginseng      * @throwsinterruptedexception -      */ the      Public Static voidMain (string[] args)throwsinterruptedexception{ + Chromestart (); A Searchbaidu (); the //chromequit (); +     } -      $     /** $ * Baidu Search -      *  -      * @throwsinterruptedexception the      */ -      Public Static voidSearchbaidu ()throwsinterruptedexception{Wuyi         /*Open Baidu Homepage*/ the Cd.get (BASEURL); -          Wu         /*Get page elements: Search Entry box*/ -Txt_search = Cd.findelement (by.id ("kw"))); About         /*Get page elements: Search button*/ $Btn_search = Cd.findelement (by.id ("su")); -          -         /*Empty the search box and enter a search term*/ - txt_search.clear (); A Txt_search.sendkeys (searchcontent); +          the         /*Click the Search button*/ - Btn_search.click (); $          the         /*wait for 1s, the page will be loaded to complete and then manipulate the element, otherwise the input box element to find error: No such element*/ theTimeUnit.SECONDS.sleep (1); the          the         /*Get page element: First search Result*/ -Lnk_first = Cd.findelement (By.xpath ("//div[@id = ' 1 ']/h3/a")); in          the System.out.println (Lnk_first.gettext ()); the         /*Click the first search results link*/ About Lnk_first.click (); the     } the      the     /** + * Chrome webdriver settings, URL and search content initialization, open Chrome browser -      */ the      Public Static voidChromestart () {Bayi         /*setting the location of Chrome Webdirver*/ theSystem.setproperty ("Webdriver.chrome.driver", "C:/windows/system32/chromedriver.exe"); the         /*Baidu Home Site assignment Value*/ -BASEURL = "http://www.baidu.com/"; -         /*Search Content Assignment*/ theSearchcontent = "Van Fongping Blog Park"; the         /*start the Chrome browser*/ thecd =Newchromedriver (); the     } -      the     /** the * Close and exit Chrome the      */94      Public Static voidChromequit () { the         /*Close Chrome*/ the cd.close (); the         /*quit Chrome*/98 cd.quit (); About     } -}
View Code

At this point, Selenium2 UI Automation Functional Test Script NO. 001 successfully completed, I hope this article can give beginners Selenium you a reference.

Finally, very grateful to the pro-stop, I hope this article can be pro helpful, warmly welcome pro reviews, thank you very much! ^_^

SELENIUM2 Learning -003-selenium2 WebUI Automation Practical example-001-100 degree search

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.