私は現在、pythonページオブジェクトメソッドを使用して申し込みテストを書いています。データベースにログインを追加し、作成したユーザーを確認してから削除したいと考えています。私はティアダウンの方法でそれを追加したいと思います。どのように私はユーザーがデータベースに作成されていることを確認して削除する方法
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import unittest
import sys
class BaseTestCase(object):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.get("https://marketplace.appdirect.com/login")
self.assertEqual("Log In | AppDirect", self.driver.title)
def tearDown(self):
if sys.exc_info()[0]:
test_method_name = self._testMethodName
# if the test fails, the code below will take a screenshot
self.driver.save_screenshot("/Users/Desktop/AppDirect/Screenshots" + test_method_name + ".png")
self.driver.quit()
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from abc import abstractmethod
class LocatorMode:
XPATH = "xpath"
CSS_SELECTOR = "cssSelector"
NAME = "name"
ID = "id"
TAG_NAME = "tagName"
class BasePage(object):
def __init__(self, driver):
self.driver = driver
@abstractmethod
def _verify_page(self):
"""
This method verifies that we are on the correct page.
"""
return
def wait_for_element_visibility(self, waitTime, locatorMode, Locator):
element = None
if locatorMode == LocatorMode.ID:
element = WebDriverWait(self.driver, waitTime).\
until(EC.visibility_of_element_located((By.ID, Locator)))
elif locatorMode == LocatorMode.NAME:
element = WebDriverWait(self.driver, waitTime).\
until(EC.visibility_of_element_located((By.NAME, Locator)))
elif locatorMode == LocatorMode.XPATH:
element = WebDriverWait(self.driver, waitTime).\
until(EC.visibility_of_element_located((By.XPATH, Locator)))
elif locatorMode == LocatorMode.CSS_SELECTOR:
element = WebDriverWait(self.driver, waitTime).\
until(EC.visibility_of_element_located((By.CSS_SELECTOR, Locator)))
else:
raise Exception("Unsupported locator strategy.")
return element
def wait_until_element_clickable(self, waitTime, locatorMode, Locator):
element = None
if locatorMode == LocatorMode.ID:
element = WebDriverWait(self.driver, waitTime).\
until(EC.element_to_be_clickable((By.ID, Locator)))
elif locatorMode == LocatorMode.NAME:
element = WebDriverWait(self.driver, waitTime).\
until(EC.element_to_be_clickable((By.NAME, Locator)))
elif locatorMode == LocatorMode.XPATH:
element = WebDriverWait(self.driver, waitTime).\
until(EC.element_to_be_clickable((By.XPATH, Locator)))
elif locatorMode == LocatorMode.CSS_SELECTOR:
element = WebDriverWait(self.driver, waitTime).\
until(EC.element_to_be_clickable((By.CSS_SELECTOR, Locator)))
else:
raise Exception("Unsupported locator strategy.")
return element
def find_element(self, locatorMode, Locator):
element = None
if locatorMode == LocatorMode.ID:
element = self.driver.find_element_by_id(Locator)
elif locatorMode == LocatorMode.NAME:
element = self.driver.find_element_by_name(Locator)
elif locatorMode == LocatorMode.XPATH:
element = self.driver.find_element_by_xpath(Locator)
elif locatorMode == LocatorMode.CSS_SELECTOR:
element = self.driver.find_element_by_css_selector(Locator)
else:
raise Exception("Unsupported locator strategy.")
return element
def fill_out_field(self, locatorMode, Locator, text):
self.find_element(locatorMode, Locator).clear()
self.find_element(locatorMode, Locator).send_keys(text)
def click(self, waitTime, locatorMode, Locator):
self.wait_until_element_clickable(waitTime, locatorMode, Locator).click()
class IncorrectPageException(Exception):
"""
This exception is raised when we try to instantiate the wrong page.
"""
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import unittest
import sys
class SignupPage(BasePage):
def clickSignupButton(self. driver):
self.click(10, "xpath", "//a[contains(.,'Sign Up')]")
self.assertEqual("Sign Up for AppDirect", self.driver.title)
def setEmail(self, email):
self.fill_out_field("id", "id_email", email)
self.click(10, "xpath", "//button[@name='userSignupButton']")
def setName(self, first, last):
self.fill_out_field("id", "id_first_name", first)
self.fill_out_field("id", "id_last_name", last)
def setPassword(self, password):
self.fill_out_field("id", "id_password", password)
self.fill_out_field("id", "id_password_confirmation", password)
def setProductName(self, name):
self.fill_out_field("id", "id_first_product_name", name)
def submit(self):
self.click(10, "cssSelector", "#create_account_form button")
from selenium import webdriver
import unittest
from BaseTestCase import BaseTestCase
from BasePage import BasePage
from SignupPage import SignupPage
class SignupTest(BaseTestCase, unittest.TestCase):
def setUp(self):
super(SignupTest, self).setUp()
# Positive Test Case
def test_SignupTest(self):
# enter the username, password, email etc
def tearDown(self):
super(SignupTest, self).tearDown()
if __name__ == "__main__":
unittest.main()
は、私はティアダウン方式でそれを追加することは偉大なコードになることを知って、より堅牢なテストをするために、SQLを使用してそれを達成することができます。何かご意見は???
私がテストの目的を誤解しない限り、あなたはティアダウンの方法でこれを望んでいません。それはテストの一部である検証です。ユーザーがdbで作成されていない場合はどうなりますか?テストが失敗したことを示すものではないでしょうか? – JeffC
あなたが投稿したコードは実際には実際の質問とは関係ないと言われています。データベースを照会するために使用しているSQLメソッドの[mcve]を作成し、ユーザーの存在を返すなどして、なぜ動作していないのかを理解するのを手伝ってください。 – JeffC