原创

使用python和selenium进行页面自动化测试的代码

1、安装selenium

pip install selenium

2、示例代码

# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
from selenium import *

from selenium import webdriver
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.mouse_button import MouseButton
from time import sleep


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press ⌘F8 to toggle the breakpoint.


def test_basic_options():
    options = EdgeOptions()
    driver = webdriver.Edge(options=options)
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
    sleep(2)
    draggable = driver.find_element(By.CLASS_NAME, 'iconsingle-line-text')
    start = draggable.location
    # finish = {'x': 33, 'y': 114};
    i = 10
    action = ActionChains(driver)
    for i1 in range(i):
        finish = {'x': 404, 'y': 404 + (20 * i1)};
        action.drag_and_drop_by_offset(draggable, finish['x'] - start['x'], finish['y'] - start['y'])

    draggable = driver.find_element(By.CLASS_NAME, 'iconmultiline-text')
    start = draggable.location
    # finish = {'x': 33, 'y': 114};
    i = 10
    for i1 in range(i):
        finish = {'x': 504, 'y': 404 + (20 * i1)};
        action.drag_and_drop_by_offset(draggable, finish['x'] - start['x'], finish['y'] - start['y'])
    action.perform()
    sleep(5)


# driver.quit()


def test_headless():
    options = EdgeOptions()
    options.add_argument("--headless=new")

    driver = webdriver.Edge(options=options)
    driver.get('http://selenium.dev')

    driver.quit()


def test_click_and_hold(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    clickable = driver.find_element(By.ID, "clickable")
    ActionChains(driver) \
        .click_and_hold(clickable) \
        .perform()

    sleep(0.5)
    assert driver.find_element(By.ID, "click-status").text == "focused"


def test_click_and_release(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    clickable = driver.find_element(By.ID, "click")
    ActionChains(driver) \
        .click(clickable) \
        .perform()

    assert "resultPage.html" in driver.current_url


def test_right_click(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    clickable = driver.find_element(By.ID, "clickable")
    ActionChains(driver) \
        .context_click(clickable) \
        .perform()

    sleep(0.5)
    assert driver.find_element(By.ID, "click-status").text == "context-clicked"


def test_back_click_ab(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
    driver.find_element(By.ID, "click").click()
    assert driver.title == "We Arrive Here"

    action = ActionBuilder(driver)
    action.pointer_action.pointer_down(MouseButton.BACK)
    action.pointer_action.pointer_up(MouseButton.BACK)
    action.perform()

    assert driver.title == "BasicMouseInterfaceTest"


def test_forward_click_ab(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
    driver.find_element(By.ID, "click").click()
    driver.back()
    assert driver.title == "BasicMouseInterfaceTest"

    action = ActionBuilder(driver)
    action.pointer_action.pointer_down(MouseButton.FORWARD)
    action.pointer_action.pointer_up(MouseButton.FORWARD)
    action.perform()

    assert driver.title == "We Arrive Here"


def test_double_click(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    clickable = driver.find_element(By.ID, "clickable")
    ActionChains(driver) \
        .double_click(clickable) \
        .perform()

    assert driver.find_element(By.ID, "click-status").text == "double-clicked"


def test_hover(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    hoverable = driver.find_element(By.ID, "hover")
    ActionChains(driver) \
        .move_to_element(hoverable) \
        .perform()

    assert driver.find_element(By.ID, "move-status").text == "hovered"


def test_move_by_offset_from_element(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    mouse_tracker = driver.find_element(By.ID, "mouse-tracker")
    ActionChains(driver) \
        .move_to_element_with_offset(mouse_tracker, 8, 0) \
        .perform()

    coordinates = driver.find_element(By.ID, "relative-location").text.split(", ")
    assert abs(int(coordinates[0]) - 100 - 8) < 2


def test_move_by_offset_from_viewport_origin_ab(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    action = ActionBuilder(driver)
    action.pointer_action.move_to_location(8, 0)
    action.perform()

    coordinates = driver.find_element(By.ID, "absolute-location").text.split(", ")

    assert abs(int(coordinates[0]) - 8) < 2


def test_move_by_offset_from_current_pointer_ab(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    action = ActionBuilder(driver)
    action.pointer_action.move_to_location(6, 3)
    action.perform()

    ActionChains(driver) \
        .move_by_offset(13, 15) \
        .perform()

    coordinates = driver.find_element(By.ID, "absolute-location").text.split(", ")

    assert abs(int(coordinates[0]) - 6 - 13) < 2
    assert abs(int(coordinates[1]) - 3 - 15) < 2


def test_drag_and_drop_onto_element(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    draggable = driver.find_element(By.ID, "draggable")
    droppable = driver.find_element(By.ID, "droppable")
    ActionChains(driver) \
        .drag_and_drop(draggable, droppable) \
        .perform()

    assert driver.find_element(By.ID, "drop-status").text == "dropped"


def test_drag_and_drop_by_offset(driver):
    driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')

    draggable = driver.find_element(By.ID, "draggable")
    start = draggable.location
    finish = driver.find_element(By.ID, "droppable").location
    ActionChains(driver) \
        .drag_and_drop_by_offset(draggable, finish['x'] - start['x'], finish['y'] - start['y']) \
        .perform()

    assert driver.find_element(By.ID, "drop-status").text == "dropped"


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # options = EdgeOptions()
    # driver = webdriver.Edge(options=options)
    # test_drag_and_drop_by_offset(driver)
    test_basic_options()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
正文到此结束
本文目录