<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 9AB WebDriver – 使用動作類 > 原文: [https://javabeginnerstutorial.com/selenium/9bb-webdriver-actions-class/](https://javabeginnerstutorial.com/selenium/9bb-webdriver-actions-class/) 燈光,攝像頭,動作! 是的,今天所有有關動作的內容。 哦,我不是在談論您在電影中觀看的那些戰斗順序,而是在談論鍵盤和鼠標的動作。 Selenium WebDriver 提供了一個面向用戶的 API,用于執行復雜的用戶手勢。 我們到處都希望自動化! 因此,除了直接使用鍵盤和鼠標,我們還可以使用`Actions`類來執行基本的`click`,`sendKeys`和復雜的操作,例如`dragAndDrop`等。 ## 一些可用的鍵盤和鼠標方法是 ### 1\. `click(WebElement target)` 參數:`target` – 要單擊的元素。 說明:單擊給定目標的中間。 ### 2\.` clickAndHold()` 說明:單擊當前鼠標位置不會釋放。 ### 3\. `doubleClick(WebElement target)` 參數:`target` – 要點擊的元素。 說明:在給定目標元素的中間雙擊。 等效于:`Actions.moveToElement(element).doubleClick();` ### 4\. `dragAndDrop(WebElement src, WebElement dest)` 參數: `source` – 要拖動的元素,即模擬按鈕按下的位置 `target` – 在以下位置移動并釋放鼠標的元素 說明:在源元素的位置單擊并按住,移至目標元素的位置,然后釋放鼠標。 ### 5\. `dragAndDropBy(WebElement src, int xOffset, int yOffset)` 參數: `source` – 模擬按鈕按下的元素。 `xOffset` - 水平移動偏移。 `yOffset` - 垂直移動偏移。 說明:在源元素的位置單擊并按住,在 x 軸上移動給定的`xOffset`,在 y 軸上移動給定的`yOffset`,然后釋放鼠標。 ### 6\. `keyDown(java.lang.CharSequencemodifier_key)` 參數: `Modifier_key` - 可以是`Keys.SHIFT`,`Keys.ALT`或`Keys.CONTROL`。 如果提供的按鍵都不是,則拋出`IllegalArgumentException`。 說明:執行修改鍵,但不釋放鍵。 隨后的交互可以假定按鍵被按下。 請注意,修飾鍵永遠不會隱式釋放-必須調用`keyUp(theKey)`或`sendKeys(Keys.NULL)`才能釋放修飾鍵。 ### 7\. `moveByOffset(int xOffset, int yOffset)` 參數: `xOffset` - 水平偏移。 負值表示將鼠標左移。 `xOffset` - 垂直偏移。 負值表示向上移動鼠標。 說明:將鼠標從其當前位置(或`0, 0`)移動 x 軸上的給定`xOffset`和 y 軸上的`yOffset`。 ### 8\. `moveToElement(WebElement target)` 參數: `target` - 要移動的元素。 說明:將鼠標移到目標元素的中間。 ### 9\. `perform()` 描述:執行或執行所有動作。 ### 10\. `release()` 說明:在當前鼠標位置釋放按下的鼠標左鍵。 ## 概覽 讓我們看一個測試案例,實現我們剛才討論的一些方法, ### 場景 1. 開啟 Chrome 瀏覽器 2. 導航到[演示站點](https://chandanachaitanya.github.io/selenium-practice-site/) 3. 使用`Actions`類執行以下鍵盤和鼠標操作, 1. 找到并點擊“自行車”復選框 2. 找到文本“消息”,然后雙擊以突出顯示它 3. 在大寫文本框中輸入“`hi there`” 4. 將`click()`從第一位置拖放到可排序列表的第三位置 4. 在 Chrome 瀏覽器中驗證輸出 5. 驗證 Eclipse IDE 控制臺輸出屏幕上是否有任何已打印的消息,并驗證 JUnit 窗格是否成功 此方案的 JUnit 代碼是, ```java import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class TestActions { // Declaring variables private WebDriver driver; private String baseUrl; @Before public void setUp() throws Exception { // System property set up for Chrome driver System.setProperty("webdriver.chrome.driver", "browser-drivers\\chromedriver.exe"); // Create a new instance for the class ChromeDriver // that implements WebDriver interface driver = new ChromeDriver(); // Implicit wait for 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Assign the URL to be invoked to a String variable baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/"; } @Test public void testKeyboardAndMouseActions() throws Exception { // Open baseUrl in Chrome browser window driver.get(baseUrl); // Locate 'Bicycle' checkbox using name WebElement bicyle = driver.findElement(By.name("vehicle1")); // Locate 'Message' textbox using id WebElement messageTextBox = driver.findElement(By.id("enterText")); // Locate 'click()' using id WebElement element = driver.findElement(By.id("click")); //Create an instance of the Actions Class Actions actions = new Actions(driver); // Perform multiple actions // Type 'hi there' in uppercase in the text box actions .moveToElement(messageTextBox) .keyDown(Keys.SHIFT) .sendKeys(messageTextBox, "hi there") .keyUp(Keys.SHIFT) .perform(); // Click 'Bicycle' checkbox actions .click(bicyle) .perform(); // Print a message to console System.out.println("Bicylce checkbox clicked."); // Double click/highlight the text, 'Message' actions .moveToElement(driver.findElement(By.id("labelText"))) .doubleClick() .perform(); // Print the text of the element that will be dragged and dropped System.out.println("Element that is dragged : " + element.getText()); // Drag and drop 'click()' using Actions class actions .dragAndDropBy(element, 50, 100) .perform(); } // End of @Test @After public void tearDown() throws Exception { // Close the Chrome browser driver.close(); System.out.println("Closing the driver"); } } ``` ### 說明: 讓我們破譯一些復雜的動作。 #### 1.突出顯示“消息”文本: ```java actions .moveToElement(driver.findElement(By.id("labelText"))) .doubleClick() .perform(); ``` 將焦點移至“消息”文本,然后雙擊以突出顯示。 使用`perform()`方法執行操作。 #### 2.在文本框中以大寫字母鍵入“`hi there`” ```java actions .moveToElement(text) .keyDown(Keys.SHIFT) .sendKeys(text, "hi there") .keyUp(Keys.SHIFT) .perform(); ``` 首先將焦點移至文本框,按住`SHIFT`鍵,鍵入文本“`hi there`”,使其大寫,然后在`keyup`方法的幫助下釋放`SHIFT`鍵。 ### 3.拖放“`click()`” 這可以通過兩種方式完成: ```java actions .dragAndDropBy(element, 50, 100) .perform(); ``` 最簡單的方法是使用`dragAndDropBy`方法,在該方法中,我們可以指定要拖動的元素以及`xOffset`和`yOffset`。 如果您想走復雜路線,看起來像忍者, ```java actions .clickAndHold(element) .moveByOffset(50, 100) .release() .perform(); ``` 單擊目標元素,在本例中為`html()`,并確保不要釋放該喀噠聲。 移動給定的 xOffset 和 yOffset,然后釋放鼠標單擊,以便將元素放置在新位置。 結果,該元素被拖放。 ### 執行結果: 為每行代碼提供了注釋,使其其余部分不言自明。 散布演示站點輸出的一些視覺效果, ![Actions output 1](https://img.kancloud.cn/61/f5/61f52a653972a50a270f7f0998557917_727x338.png) 自行車復選框位于并按預期檢查。 ![Actions output 2](https://img.kancloud.cn/ff/2d/ff2d5d65e68a7d6dbe88b13d33533343_764x405.png) 雙擊文本`Message`,結果將突出顯示。 在文本框中,“`hi there`”用大寫字母鍵入。 從可排序列表中將`click()`從其默認的第一位置拖動到第三位置。 在 JUnit 窗格中,綠色條顯示測試用例已成功執行。 同樣,控制臺窗口顯示沒有錯誤以及所有打印的消息如預期的那樣。 ![Actions eclipse IDE output](https://img.kancloud.cn/c1/35/c135698fbd24a0bc79360239e24e7b00_637x323.png) 是時候把忍者帶出來了。 嘗試使用盡可能多的動作類方法,并從中獲得樂趣! 上面討論的所有代碼都可以在 [GitHub 倉庫](https://github.com/JBTAdmin/Selenium)的“WebDriver”文件夾中找到。 您可以為倉庫加注星標和分支以方便使用。 請仔細閱讀`README.md`文件以獲取明確說明。 祝你有美好的一天!
                  <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>

                              哎呀哎呀视频在线观看