Section sixteen, 6-xml parametric case and parallel

First, read the xml file parameters

1, case presentations --- "create a class needs to read data

. 1  Package testclasses;
 2  
. 3  Import org.testng.annotations.Test;
 . 4  
. 5  Import org.testng.annotations.BeforeClass;
 . 6  Import org.testng.annotations.Parameters;
 . 7  Import org.testng.annotations.AfterClass;
 . 8  
. 9  public  class {TestNG_Parameters
 10      
. 11      @BeforeClass
 12 is  //     @Parameters Remark xml parameter indicates the need to read the need to import (import org.testng.annotations.Parameters;) package 
13 is      @Parameters ({ "Browser", "Platform" } )
 14  //    Xml file in order to receive the value, so we need to define two process variables 
15      public  void the setUp (the Browse String, String Platform) {
 16          System.out.println ( "TestNG_Parameters -> Steup" );
 . 17          the System.out .println ( "value. 1 from the Parameters XML File:." + the Browse);
 18 is          System.out.println ( "value from the Parameters XML File 2:." + Platform);
 . 19      }
 20 is      
21 is    @Test
 22 is    @Parameters ({ " Response " })
 23 is    public  void testMethod1 (Response String) {
 24          System.out.println (" TestNG_Parameters -> testMethod1 "+response);
25         System.out.println("Response from xml file:");
26   }
27 
28   @AfterClass
29   public void afterClass() {
30   }
31 }

Xml configuration file

1 <! - this may be given when there is no run-line configuration ->
 2 <DOCTYPE the SYSTEM Suite "http://testng.org/testng-1.0.dtd"!>
 . 3 <Suite name = "the TestSuite the Parameters">
 . 4      <test name = "the test. 1">
 . 5      <-! configuration parameters here, to read data at the time of testing, value indicating the specific assignment ->
 . 6      <parameter name = "Browser" value = " Firefox "> </ Parameter>
 . 7      <Parameter name =" Platform "value =" Windows "> </ Parameter>
 . 8      <Parameter name =" Response "value =" 201 "> </ Parameter>
 . 9          <classes>
 10              <class name="testclasses.TestNG_Parameters"></class>
11         </classes>
12     </test>
13 </suite>

operation result

 

 2, when the xml through a plurality of parameter values, we only need to read one, then it can be split into an array, then read.

Package testclasses; 

Import org.testng.annotations.Test; 

Import org.testng.annotations.BeforeClass;
 Import org.testng.annotations.Parameters;
 Import org.testng.annotations.AfterClass; 

public  class TestNG_Parameters { 
    
    @BeforeClass 
//     @Parameters Remarks xml represents the need to read the parameters need to import (import org.testng.annotations.Parameters;) package 
    @Parameters ({ "Browser", "Platform" })
 //     order to receive the value in the xml file, so in the process, we need to define two variables 
    public  void the setUp (the Browse String, String Platform) { 
        System.out.println ( "TestNG_Parameters -> Steup" );
        System.out.println ( "value from the Parameters 1. XML File:" + the Browse); 
        System.out.println ( "value from the Parameters 2. XML File:" + Platform); 
    } 
    
  @Test 
  @Parameters ({ "Response" })
   public  void testMethod1 (Response String) {
 //       the value is split in plurality xml value
 //       split the method may according to the values specified rule split, where we each predetermined interval is a comma value array 
      String [] = response.split the StringArray ( "," ); 
        System.out.println ( "TestNG_Parameters -> testMethod1" ); 
        System.out.println ( "XML File from the Response:" + Response);
//        Read the second bit value, an expected result of "202", the index starts from 0 
        System.out.println ( "XML from the StringArray the Response [. 1] File:" + the StringArray [. 1 ]); 
  } 
}

xml configuration:

1 <! - this may be given when there is no run-line configuration ->
 2 <DOCTYPE the SYSTEM Suite "http://testng.org/testng-1.0.dtd"!>
 . 3 <Suite name = "the TestSuite the Parameters">
 . 4      <test name = "the test. 1">
 . 5      <-! configuration parameters here, to read data at the time of testing, value indicating the specific assignment ->
 . 6      <parameter name = "Browser" value = " Firefox "> </ Parameter>
 . 7      <Parameter name =" Platform "value =" Windows "> </ Parameter>
 . 8      <-! 201 is a string type, numeric type if you want changes in the force required class java conversion ->
 . 9      <Parameter name = "Response" value = "201"> </ Parameter>
 10      <!
- If there are multiple values, we can split the value, wherein the value of the specified position is read -> . 11      <Parameter name = "Response" value = "201, 202"> </ Parameter>
 12 is         <classes>
13             <class name="testclasses.TestNG_Parameters"></class>
14         </classes>
15     </test>
16 </suite>

Operating results as follows:

 Note: xml The parameters are character types, if we need int type in the code, then the process of casting.

 

 

Second, the case with the parallel execution testNG

testNG in the implementation of case, the default in the order each execution of only one case, if we need more case executed simultaneously, then the following configuration is required on the label in xml suite.

Scenario demonstrates --- "For example, we have two test class needs to execute in parallel:

第一个类
package
testclasses1; import org.testng.annotations.BeforeClass; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class TestNG_Parallel1 { @Test public void testMethond1() throws InterruptedException { System.out.println("TestNG_Parallel1 -> testMethond1"); Thread.sleep(6000); System.out.println("TestNG_Parallel1 -> testMethond1 -> More Steps"); } @Test public void testMethond2() throws InterruptedException { System.out.println("TestNG_Parallel1 -> testMethond2"); Thread.sleep(6000); System.out.println("TestNG_Parallel1 -> testMethond2 -> More Steps"); } }
第二个类
package
testclasses1; import org.testng.annotations.Test; public class TestNG_Parallel2 { @Test public void testMethond1() throws InterruptedException { System.out.println("TestNG_Parallel2 -> testMethond1"); Thread.sleep(6000); System.out.println("TestNG_Parallel2 -> testMethond1 -> More Steps"); } @Test public void testMethond2() throws InterruptedException { System.out.println("TestNG_Parallel2 -> testMethond2"); Thread.sleep(6000); System.out.println("TestNG_Parallel2 -> testMethond2 -> More Steps"); } }

Xml configuration file:

<! - This line can not configure the runtime error ->
<DOCTYPE the SYSTEM Suite "http://testng.org/testng-1.0.dtd"!>
<! - Parallel = "Tests" is used to specify that a class of tags can be performed in parallel, fill here "tests", represents the tests will be performed in parallel, may be provided to fill classes or class, methods and the like are executed in parallel ->
<-! Thread-COUNT = "2" represents a set of concurrent number 2 ->
<Suite name = "Parallel the TestSuite" Parallel = "Tests" Thread-COUNT = "2">
<Test name = "the Test. 1">
<classes>
<class name = "testclasses1.TestNG_Parallel1"> < / class>
</ classes>
</ Test>
<Test name = "the Test 2">
<classes>
<class name = "testclasses1.TestNG_Parallel2"> </ class>
</ classes>
</ Test>
</suite>

operation result:

 

 

Third, the scene demonstrates with examples

 1 package testclasses1;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 import org.openqa.selenium.By;
 6 import org.openqa.selenium.WebDriver;
 7 import org.openqa.selenium.WebElement;
 8 import org.openqa.selenium.chrome.ChromeDriver;
 9 import org.openqa.selenium.ie.InternetExplorerDriver;
10 import org.testng.annotations.AfterClass;
11 import org.testng.annotations.BeforeClass;
12 import org.testng.annotations.Parameters;
13 Import org.testng.annotations.Test;
 14  
15  / * Scene:
 16  * 1. Open the website: https://www.migang.com 
17  * 2. Log link and click on it to find
 18  * 3. Find the user name and password text box
 19  * 4. the user name and password in the text box to enter the value
 20  * 5 clicks the login button
 21  * Example 6. the use of parallel execution in this IE and Chrome * / 
22 is  
23 is  public  class TestNG_SeleniumParametersParallelExample {
 24      
25      the WebDriver Driver;
 26 is      String URL;
 27   
28       @BeforeClass
 29       @Parameters ({ "BaseURL", "the Browse" })
 30       public void beforeClass(String baseurl,String browse) {
31 //         .equalsIgnoreCase:该方法判断是否相等,忽略大小写
32          if(browse.equalsIgnoreCase("IE")) {
33               driver = new InternetExplorerDriver();
34          }else if(browse.equalsIgnoreCase("Chrome")){
35               driver = new ChromeDriver();
36          }
37           driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
38           driver.manage().window().maximize();
39           driver.get(baseurl);
40       }
41      
42      @Test
43       @Parameters({"login","username","password","dlan","yfm","mm"})
44       public void Chrome(String login,String username,String password,String dlan,String yfm,String mm) {
45           WebElement denglu = driver.findElement(By.className(login));
46               denglu.click();
47           WebElement name = driver.findElement(By.name(username));
48             name.sendKeys(yfm);
49           WebElement pasord = driver.findElement(By.name(password));
50               pasord.sendKeys(mm);
51           WebElement button = driver.findElement(By.name(dlan));
52               button.click();
53       }
54      
55      @AfterClass
56       public void afterClass() throws InterruptedException {
57           Thread.sleep(6000);
58           driver.quit();
59       }
60 }

xml configuration:

 1 <!-- 没有此行配置运行时可能会报错 -->
 2 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
 3 <suite name="Parameters" parallel="tests" thread-count="2">
 4     <test name = "IE Parameters" >
 5         <parameter name="browse" value="IE"></parameter>
 6         <parameter name="baseurl" value="https://www.migang.com"></parameter>
 7         <parameter name="login" value="login"></parameter>
 8         <parameter name="username" value="username"></parameter>
 9         <parameter name="password" value="password"></parameter>
10         <parameter name="dlan" value="loginnow"></parameter>
11         <parameter name="yfm" value="1111111111"></parameter>
12         <parameter name="mm" value="111111"></parameter>
13         <classes>
14             <class name="testclasses1.TestNG_SeleniumParametersParallelExample"></class>
15         </classes>
16     </test>
17     <test name = "Chrome Parameters">
18         <parameter name="browse" value="Chrome"></parameter>
19         <parameter name="baseurl" value="https://www.migang.com"></parameter>
20         <parameter name="login" value="login"></parameter>
21         <parameter name="username" value="username"></parameter>
22         <parameter name="password" value="password"></parameter>
23         <parameter name="dlan" value="loginnow"></parameter>
24         <parameter name="yfm" value="1111111111"></parameter>
25         <parameter name="mm" value="111111"></parameter>
26         <classes>
27             <class name="testclasses1.TestNG_SeleniumParametersParallelExample"></class>
28         </classes>
29     </test>
30 </suite>

 

 

 

If you do not understand the small partners can be added to group "555 191 854" I asked, the group is small software industry partners together to learn from each other.

Content with coherence, unlabeled place to see the previous blog, which is a set of automated content on ava selenium +, starting with java foundation.

Welcome attention, please indicate the source.

Guess you like

Origin www.cnblogs.com/luohuasheng/p/11448245.html