我目前使用python页面对象方法编写了我的注册测试。我想添加登录到数据库并检查我创建的用户,然后将其删除。我想在我的拆解方法中添加它。如何使用sql实现它以进行更强大的测试
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()
我知道在拆解方法中添加它将是一个很棒的代码。任何想法???
答案 0 :(得分:0)
SQL就是这样的:
Delete From <table_name> Where <Unique_Column> = <Value>
其中:
UserId
37
例如:
Select * from users where userId = 37
如果从代码运行此查询,则回调将显示此操作影响的行数,这意味着0 =未找到用户,否则您找到了结果。