Python Selenium:如何重用另一个类中的一个类

尼帕特

我是Python和Selenium的新手。我需要编写一个登录模块,该模块可以在测试用例中重用。这是我的2个文件。我需要帮助来调用登录模块,以便浏览器启动并且用户可以登录。然后,第二个模块启动,测试用例开始(在同一浏览器中)。我在2个单独的文件中写了2个班级。我的代码如下:

mylogin.py文件

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 as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
import unittest
class myLoginclass(unittest.TestCase):
    @classmethod   
    def test_TC_1_login_page(self):       
        self.driver = webdriver.Firefox()
        self.driver.get(http://www.gmail.com)
        self.driver.find_element_by_xpath(".//*[@id='name-group']/input").send_keys("HELLO")
        self.driver.find_element_by_xpath(".//*[@id='password-group']/input").send_keys("WORLD")
        self.driver.find_element_by_id("loginButton").click()

if __name__ == '__main__':
    unittest.main(failfast=True, exit=False)

myorder.py文件:

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 as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
import unittest
from mylogin import myLoginclass
class myorderclass(unittest.TestCase):
    @classmethod   
    def test_TC_2_orderProcess(self):       
        self.driver.find_element_by_xpath("".//*[@id='aoTkt']/div/div")).click()
        self.driver.find_element_by_xpath(".//*[@id='presales']/input").click()
        self.driver.find_element_by_link_text("DISPATCH").click()
        self.driver.find_element_by_id("submitButton").click()

if __name__ == '__main__':
    unittest.main(failfast=True, exit=False)
萨希尔·阿加瓦尔

使用类继承。根据您的用例,使用setUp()或setUpClass()调用登录函数。

from mylogin import myLoginclass

class myorderclass(myLoginclass):
    def setUp(self):
        self.test_TC_1_login_page()

用于多个测试的Unittest setUp / tearDown

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Python的另一个类中定义一个类是否有好处?

如何从Python中的另一个文件调用类方法?

如何使Abstract类从Python中的另一个Abstract类继承?

从python中的另一个类访问self

python / kivy:如何从.kv文件中的另一个类传递文本值

Python-如何从另一个新修改的python文件中调用类方法

Python从类中的另一个函数调用一个函数

在一个类中调用一个函数,在另一个类python中调用一个函数

如何从另一个目录导入python类

如何从另一个文件夹中的文件导入python类?

从Python 2.7中的另一个类方法调用一个类方法

如何在Python中的类中对另一个函数使用一个函数

如何使一个继承自python中另一个类的类的构造函数?

在Python中从一个类到另一个类访问变量

Selenium / Python:如何在与另一个类处于同一级别的某个类中捕获按钮的所有实例?

如何在python中为另一个类定义一个包含类?

在python的同一类中的另一个方法中调用一个类的方法

从python中的另一个类调用变量

Python3如何从另一个类中的一个类调用方法

如何从另一个类正确访问一个类的 StringVar() - Python - tkinter

如何从另一个 python 文件中的一个 python 文件中的类调用方法

Python类:一个类中的实例方法指向另一个类中的实例方法

构造一个类在另一个文件python中的对象

python中访问另一个类的方法

Python:在另一个类中调用一个类

如何在同一个类中的另一个 api 中调用 python api 方法?

Python如何在另一个类方法中使用一个类

基于Python中的另一个对象属性创建一个类的对象

如何在 Python 中的另一个类中使用一个类中的变量?