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