<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Selenium 中的并行執行:會話處理& TestNG 依賴性 > 原文: [https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html](https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html) 要了解如何并行運行腳本,讓我們首先了解 ## 為什么我們需要會話處理? 在測試執行期間,Selenium WebDriver 必須一直與瀏覽器進行交互以執行給定的命令。 在執行時,也有可能在當前執行完成之前,其他人在同一臺機器和相同類型的瀏覽器中開始執行另一個腳本。 ![Parallel Execution & Session Handling in Selenium](https://img.kancloud.cn/57/c4/57c4b55351d7e3702158c704636527e1_536x375.png "Sessions, Parallel run and Dependency in Selenium") 在這種情況下,我們需要一種機制,通過這種機制我們的兩個不同的執行不應相互重疊。 這可以使用 Selenium 中的會話處理來實現。 ## 如何在 Selenium WebDriver 中實現會話處理? 如果檢查 Selenium WebDriver 的源代碼,則會找到一個名為“ sessionId”的變量。 每當我們創建 WebDriver 對象的新實例時,都會生成一個新的'sessionId'并將其附加到特定的 Firefox / Chrome / IE 驅動程序()。 ![Parallel Execution & Session Handling in Selenium](https://img.kancloud.cn/7a/05/7a05f84a0132c5b11a593c45199ef3b8_371x135.png "Sessions, Parallel run and Dependency in Selenium") 因此,我們在此之后所做的所有操作將僅在特定的 Firefox 瀏覽器會話中執行。 ![Parallel Execution & Session Handling in Selenium](https://img.kancloud.cn/f1/6f/f16fc226b91e413ad6dd4b62d108b22c_478x258.png "Sessions, Parallel run and Dependency in Selenium") 由于這是內置功能,因此無需明確分配會話 ID **代碼示例:**此處將為兩個不同的 WebDriver 生成兩個不同的會話。 ``` import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class SessionHandling { public static void main(String...strings ){ //First session of WebDriver WebDriver driver = new FirefoxDriver(); //Goto guru99 site driver.get("http://demo.guru99.com/V4/"); //Second session of WebDriver WebDriver driver2 = new FirefoxDriver(); //Goto guru99 site driver2.get("http://demo.guru99.com/V4/"); } } ``` ## 如何使用 Selenium 運行并行測試 在某些情況下,您想同時運行多個測試。 在這種情況下,可以使用“平行”屬性 ![Parallel Execution & Session Handling in Selenium](https://img.kancloud.cn/75/3e/753e0459bdcd1fb3effa4f492586a2ba_505x176.png "Sessions, Parallel run and Dependency in Selenium") 套件標簽的 parallel 屬性可以接受四個值: <colgroup><col> <col></colgroup> | 測試 | [xml 文件的< test >標簽內的所有測試用例將并行運行。](/software-testing.html) | | 類 | [Java](/java-tutorial.html) 類中的所有測試用例將并行運行 | | 方法 | 所有帶有@Test 批注的方法將并行執行。 | | 實例 | 同一實例中的測試用例將并行執行,但是兩個不同實例的兩種方法將在不同的線程中運行。 | 屬性 *thread-count* 允許您指定應為此執行分配多少個線程。 完整示例:在本示例中,三個測試用例將并行運行并在 [http://demo.guru99.com](http://demo.guru99.com) 中填充登錄數據 完整項目將如下所示: ![Parallel Execution & Session Handling in Selenium](https://img.kancloud.cn/55/fe/55fee97e45c3e34a1e13e846c83f63dd_500x189.png "Sessions, Parallel run and Dependency in Selenium") TestGuru99MultipleSession.java ``` import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class TestGuru99MultipleSession { @Test public void executSessionOne(){ //First session of WebDriver System.setProperty("webdriver.chrome.driver","chromedriver.exe"); WebDriver driver = new ChromeDriver(); //Goto guru99 site driver.get("http://demo.guru99.com/V4/"); //find user name text box and fill it driver.findElement(By.name("uid")).sendKeys("Driver 1"); } @Test public void executeSessionTwo(){ //Second session of WebDriver System.setProperty("webdriver.chrome.driver","chromedriver.exe"); WebDriver driver = new ChromeDriver(); //Goto guru99 site driver.get("http://demo.guru99.com/V4/"); //find user name text box and fill it driver.findElement(By.name("uid")).sendKeys("Driver 2"); } @Test public void executSessionThree(){ //Third session of WebDriver System.setProperty("webdriver.chrome.driver","chromedriver.exe"); WebDriver driver = new ChromeDriver(); //Goto guru99 site driver.get("http://demo.guru99.com/V4/"); //find user name text box and fill it driver.findElement(By.name("uid")).sendKeys("Driver 3"); } } ``` **TestNG.XML** ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="3" parallel="methods" > <test name="testGuru"> <classes> <class name="TestGuru99MultipleSession"> </class> </classes> </test> </suite> ``` ## 測試用例順序和依賴性 您可以設置[測試用例](/test-case.html)執行的順序和依賴性。 假設您有兩個測試用例“ testGuru99TC1”和“ testGuru99TC2”,并且您想在“ testGuru99TC1”之前執行測試用例“ testGuru99TC2”。 在這種情況下,我們將使用'dependsOnMethods'屬性進行依賴和執行順序。 ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="3" parallel="methods" > <test name="testGuru"> <classes> <class name="TestGuru99MultipleSession"> <include value="testGuru99TC1" dependsOnMethods=" testGuru99TC2"/> <include value="testGuru99TC2"/> </class> </classes> </test> </suite> ``` ## 摘要 * 為 WebDriver 的新實例創建一個新的 sessionID。 * 一個會話將與一個特定的瀏覽器綁定。 * 使用屬性線程和并行,可以并行運行腳本。 * 您可以使用屬性依賴性來設置測試執行的順序
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看