A typical example of webdriver+testng

Source: Internet
Author: User
Tags testng

Want to make the test more flexible, 1. Can be configured to use any supported browser for testing; 2. Configure all Google url;3. Configure keywords for search. The Modified code:

 Public classgoogletest {webdriver driver; @Parameters ({"Browser"}) @BeforeTest Public voidSetupbrowser (String browser) {if(Browser.equals ("Firefox") ) {driver=Newfirefoxdriver ();} Else{driver=Newchromedriver ();}} @Parameters ({"url", "keyword"}) @Test Public voidsearch (string url, string keyword, itestcontext context) {driver.get (URL); webelement element= Driver.findelement (By.name ("Q") ; element.sendkeys (keyword); Element.submit (); Assert.asserttrue (Driver.gettitle (). Contains (keyword),"Something wrong with title"); }}

Content of Testng.xml:

<suite name= "Magus Demo" verbose= "2" ><parameter name= "browser" value= "Firefox"/><parameter name= "url" Value= "http://www.google.com"/><parameter name= "keyword" value= "Magus"/><test name= "Search function" Preserve-order= "true" ><classes><class name= "test. Googletest "><methods><include name=" Setupbrowser "/><include name=" Search "/></methods> </class></classes></test></suite>
Using the @parameters tag of testng, the test method is used to read parameters from the Testng.xml and to realize parameterization. In the Testng.xml configuration, the test node needs to add the configuration of a property: Preserve-order= "true". This preserve-order default is False, and the order of execution of all methods below the node is unordered. Set it to true to ensure that the methods under the node are executed sequentially. This feature of testng allows us to assemble tests in testng.xml. Suppose we have a lot of independent testing methods, such as Navigatecategory addcomment addfriend login logout can spell out different tests in testng.xml, for example
<test name= "Add friend" preserve-order= "true" ><classes><class name= "test. Googletest "><methods><include name=" login "/><include name=" addfriend "/><include name=" Logout "/></methods></class></classes></test><test name=" ADD comment to Category "Preserve-order=" true "><classes><class name=" test. Googletest "><methods>                <include name=" Login "/><include name=" Navigatecategory "/>< Include name= "AddComment"/><include name= "Logout"/></methods></class></classes ></test>

A typical example of webdriver+testng

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.