[雪峰磁針石部落格]selenium自動化測試工具python筆試面試專案實戰5鍵盤操作

書籍尋找發表於2018-10-18

說明

本文參考答案基於Chrome,解析度1920*1080,在其他環境表現可能會不同。
本文程式碼地址

  • 參考書籍下載:

Learning Selenium Testing Tools with Python-2014.pdf

Selenium自動化測試 基於 Python 語言 – 2018.pdf

上機實操: 在新的TAB開啟連線

  • 開啟:https://china-testing.github.io/
  • 選擇”資料分析”欄目的文章
  • 按住”Ctrl+TAB”選擇”python”欄目的文章
  • 切換到新的標籤”python”
  • 關閉新的標籤”python”
  • 關閉瀏覽器

參考答案

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-10-17
import time

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome()
driver.get("https://china-testing.github.io/")
driver.implicitly_wait(30)
driver.maximize_window()
element = driver.find_element_by_link_text(`資料分析`)
element.click()
time.sleep(3)
element = driver.find_element_by_link_text(`python`)
ActionChains(driver).key_down(Keys.CONTROL).click(element).key_up(
    Keys.CONTROL).perform()
time.sleep(3)
driver.switch_to.window(driver.window_handles[1])
time.sleep(3)
driver.close() # 關閉當前TAB
time.sleep(3)
driver.quit()                                                                                            
  • 面試問答
  1. driver.quit() 和 driver.close()有什麼區別?

2.selenium中按下和鬆開鍵如何表示?

3.簡述ActionChains類的作用?

上機實操: 驗證懸浮提示內容

圖片.png

  • 滑鼠移動到上圖的”Your age:”
  • 確認懸浮提示內容為`We ask for your age only for statistical purposes.`
  • 關閉瀏覽器

    • 參考答案
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-10-17
import unittest
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains


class ToolTipTest (unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("http://jqueryui.com/tooltip/")
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()

    def test_tool_tip(self):
        driver = self.driver

        frame_elm = driver.find_element_by_class_name(`demo-frame`)
        driver.switch_to.frame(frame_elm)
        
        time.sleep(3)
        age_field = driver.find_element_by_id(`age`)
        ActionChains(self.driver).move_to_element(age_field).perform()

        time.sleep(3)
        tool_tip_elm = WebDriverWait(self.driver, 10).until(
            expected_conditions.visibility_of_element_located((
                By.CLASS_NAME, `ui-tooltip-content`)))

        # verify tooltip message
        self.assertEqual(`We ask for your age only for statistical purposes.`, 
                         tool_tip_elm.text)
        time.sleep(3)

    def tearDown(self):
        self.driver.close()

if __name__ == `__main__`:
    unittest.main(verbosity=2)                                                                                      
  • 面試問答

1.move_to_element()有什麼用途?

上機實操: 雙擊改變顏色

圖片.png

  • 雙擊上圖藍色的框,把顏色該變成黃色

    • 參考答案
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-10-18

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest


class DoubleClickTest (unittest.TestCase):

    URL = `http://api.jquery.com/dblclick/`

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get(self.URL)
        self.driver.maximize_window()

    def test_double_click(self):
        driver = self.driver
        frame = driver.find_element_by_tag_name(`iframe`)
        driver.switch_to.frame(frame)
        box = driver.find_element_by_tag_name(`div`)

        # verify color is Blue
        self.assertEqual(`rgba(0, 0, 255, 1)`,
                         box.value_of_css_property(`background-color`))

        ActionChains(driver).move_to_element(
            driver.find_element_by_tag_name(`body`)).perform()
        ActionChains(driver).double_click(box).perform()

        # verify Color is Yellow
        self.assertEqual(`rgba(255, 255, 0, 1)`,
                         box.value_of_css_property(`background-color`))

    def tearDown(self):
        self.driver.close()

if __name__ == `__main__`:
    unittest.main(verbosity=2)
  • 面試問答

1.double_click()有什麼用途?

2.rgba的含義?

上機實操: 在新的TAB開啟連線

圖片.png

  • 拖動左邊的框到右邊

參考答案

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 討論釘釘免費群21745728 qq群144081101 567351477
# CreateDate: 2018-10-18
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest


class DragAndDropTest (unittest.TestCase):

    URL = `http://jqueryui.com/resources/demos/droppable/default.html`

    def setUp(self)    :
        self.driver = webdriver.Chrome()
        self.driver.get(self.URL)
        self.driver.maximize_window()

    def test_drag_and_drop(self):
        driver = self.driver

        source = driver.find_element_by_id(`draggable`)
        target = driver.find_element_by_id(`droppable`)

        ActionChains(self.driver).drag_and_drop(source, target).perform()
        self.assertEqual(`Dropped!`, target.text)

    def tearDown(self):
        self.driver.close()

if __name__ == `__main__`:
    unittest.main(verbosity=2)
  • 面試問答

1.drag_and_drop()有什麼用途?

參考資料


相關文章