Selenium Testskript Aufbau

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
maGGTech
User
Beiträge: 21
Registriert: Donnerstag 21. Mai 2020, 12:11

Hallo zusammen, für die die schonmal mit Selenium gearbeitet haben dürfte die folgende Seite ja vertraut sein: https://selenium-python.readthedocs.io/ ... jects.html

Mein Anfang von Skript sieht jetzt so aus:

main.py

Code: Alles auswählen

import unittest
from selenium import webdriver
import page

URL = "https://.../"
CHROME_DRIVER_PATH = r"c:\Users\...\chromedriver.exe"

class Chrome(unittest.TestCase):
    """Set info text"""

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

    def test_export_picture_map(self):
        map_page = page.MapPage(self.driver)
        assert map_page.create_map_visualisation(), "Map Visualization could not be set"
        assert map_page.click_save_button(), "Save Picture Button could not be located"
        assert map_page.download_picture_file(), "Picture File could not be downloaded"

    def test_export_table(self):
        table_page = page.TablePage(self.driver)
        assert table_page.create_table_visualization(), "Table Visualization could not be set"
        assert table_page.click_save_button(), "Save Table Button could not be located"
        assert table_page.click_confirm_button(), "Confirm Button could not be located"
        assert table_page.download_csv_file(), "Csv File could not be downloaded"

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

Code: Alles auswählen

from locators import *
from selenium.webdriver.chrome.options import Options

DOWNLOAD_DIRECTORY = r"c:\Users\...\Desktop"
DOWNLOADED_PNG_DIRECTORY = r"c:\Users\...\Desktop\example_file.png"
DOWNLOADED_CSV_DIRECTORY = r"c:\Users\...\Desktop\example_file.csv"

class BasePage(object):
    """Base class to initialize the base page that will be called from all pages"""

    def __init__(self, driver):
        self.driver = driver

class MapPage(BasePage):
    """Set info text"""

    def create_map_visualisation(self):
        element_0 = self.driver.find_element(ConfigureLayoutLocators.SWITCH_LAYOUT)
        element_0.click()
        element_1 = self.driver.find_element(ConfigureLayoutLocators.ONE_X_ONE)
        element_1.click()
        element_2 = self.driver.find_element(ConfigureVisLocators.MAP)
        element_2.click()

    def click_save_button(self):
        element = self.driver.find_element(*ExportFilesLocators.PNG_MAP)
        element.click()

    @staticmethod
    def download_picture_file():
        option = Options()
        option.add_experimental_option("prefs", {
            "download.default_directory": DOWNLOADED_PNG_DIRECTORY,
            "download.prompt_for_download": False,
            "download.directory_upgrade": True,
            "safebrowsing.enabled": True})

class TablePage(BasePage):
    """Set info text"""

    def create_table_visualization(self):
        element_0 = self.driver.find_element(ConfigureLayoutLocators.SWITCH_LAYOUT)
        element_0.click()
        element_1 = self.driver.find_element(ConfigureLayoutLocators.ONE_X_ONE)
        element_1.click()
        element_2 = self.driver.find_element(ConfigureVisLocators.TABLE)
        element_2.click()

    def click_save_button(self):
        element = self.driver.find_element(*ExportFilesLocators.CSV_TABLE)
        element.click()

    def click_confirm_button(self):
        element = self.driver.find_element(*ConfirmDialogLocators.YES)
        element.click()

    @staticmethod
    def download_csv_file():
        option = Options()
        option.add_experimental_option("prefs", {
            "download.default_directory": DOWNLOADED_CSV_DIRECTORY,
            "download.prompt_for_download": False,
            "download.directory_upgrade": True,
            "safebrowsing.enabled": True})
element.py

Code: Alles auswählen

from selenium.webdriver.support.ui import WebDriverWait

class BasePageElement(object):
    """Base page class that is initialized on every page object class."""

    def __set__(self, obj, value):
        """Sets the text to the value supplied"""
        driver = obj.driver
        WebDriverWait(driver, 100).until(
            lambda driver: driver.find_element_by_name(self.locator))
        driver.find_element_by_name(self.locator).clear()
        driver.find_element_by_name(self.locator).send_keys(value)

    def __get__(self, obj, owner):
        """Gets the text of the specified object"""
        driver = obj.driver
        WebDriverWait(driver, 100).until(
            lambda driver: driver.find_element_by_name(self.locator))
        element = driver.find_element_by_name(self.locator)
        return element.get_attribute("value")
locators.py

Code: Alles auswählen

from selenium.webdriver.common.by import By

class ConfigureLayoutLocators(object):
    """A class for configure layout locators. All configure layout locators should come here"""

    SWITCH_LAYOUT = (By.XPATH, '//*[@id="..."]/div[1]/div[2]')

    ONE_X_TWO__TWO_X_ONE = (By.XPATH, '/html/body/div[3]/div/ul/ul/ul/li[1]')
    ONE_X_ONE = (By.XPATH, '/html/body/div[3]/div/ul/ul/ul/li[2]')
    ONE_X_TWO = (By.XPATH, '/html/body/div[3]/div/ul/ul/ul/li[3]')
    # ... and more locators
	

class ConfigureVisLocators(object):
    """A class for configure visualization locators. All configure visualization locators should come here"""

    MAP = (By.XPATH, '/html/body/div[1]/div[2]/div/div[2]/div[2]/main/div/div/div/div[9]/ul/li[1]/div[1]')
    TABLE = (By.XPATH, '/html/body/div[1]/div[2]/div/div[2]/div[2]/main/div/div/div/div[9]/ul/li[6]/div[1]')
    # ... and more locators

class ExportFilesLocators(object):
    """A class for export files locators. All export files locators should come here"""

    CSV_TABLE = (By.XPATH, '//*[@class="..."]/div[7]/div/div/div[2]/ul/li[1]/ul/li[2]')
    PNG_MAP = (By.XPATH, '//*[@class="..."]/div[6]/div/div/div[2]/ul/li[3]/ul/li')

class ConfirmDialogLocators(object):
    """A class for confirm dialog locators. All confirm dialog locators should come here"""

    YES = (By.XPATH, '//*[@class="dialog"]/div[3]/div[1]')
    NO = (By.XPATH, '//*[@class="dialog"]/div[3]/div[2]')
Jetzt zu meiner Frage: Ich würde gerne wissen, ob man das im Groben so macht. Und ob ihr noch Tipps habt, auf was ich achten muss.
Ich bin mir noch ziemlich unsicher bzgl. der element.py, diese wurde in dem Beispiel für das Suchen in einer TextBox verwendet und soll für alle verwendete Elemente 100 Sekunden warten.
Aus Datenschutz Gründen darf ich leider nicht die Webseite hier nennen und selbst wenn würden euch die Berechtigungen dafür fehlen. Alternativ, kennt ihr eine Webseite wo man ein Bild und eine Tabelle testweise runterladen kann?

Das Skript soll im Prinzip nur testen, ob zwei Dateien runtergeladen werden können.

Sirius3 du brauchst bitte nicht hier drauf zu antworten.
Antworten