封装selenium之三

第三种叫做UI Mapping方式

在编写脚本时可能出现这样的脚本

public void testNew() throws Exception {
driver.open("http://www.test.com");


driver.type("loginForm:tbUsername", "xxxxxxxx");
driver.click("loginForm:btnLogin");


driver.click("adminHomeForm:_activitynew");
driver.waitForPageToLoad("30000");


driver.click("addEditEventForm:_IDcancel");
driver.waitForPageToLoad("30000");


driver.click("adminHomeForm:_activityold");
driver.waitForPageToLoad("30000");
}

是不是看起来很乱,还是已经按照对元素的操作进行分隔之后的样子。将没有学过seleniumAPI会觉得难以理解的部分替换掉:

public void testNew() throws Exception {

driver.open("http://www.test.com");


driver.type(admin.username, "xxxxxxxx");

driver.click(admin.loginbutton);


driver.click(admin.events.createnewevent);

driver.waitForPageToLoad("30000");


driver.click(admin.events.cancel);

driver.waitForPageToLoad("30000");


driver.click(admin.events.viewoldevents);
driver.waitForPageToLoad("30000");

}

The locators will still refer to HTML objects, but we have introduced a layer of abstraction between the test script and the UI elements. Values are read from the properties file and used in the Test Class to implement the UI Map. 

之后需要建立properties文件,然后将你的变量与实际的元素位置相对应,实现变量与元素的相对应,通过解析配置文件,将元素值传入对应的方法中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
封装Selenium基类是一种常见的测试框架设计模式,它可以提供一些通用的方法和属性,以便在测试过程中更方便地使用Selenium库。下面是一个简单的封装Selenium基类的示例: ```python from selenium import webdriver class BasePage: def __init__(self, driver): self.driver = driver def open_url(self, url): self.driver.get(url) def find_element(self, locator): return self.driver.find_element(*locator) def click(self, locator): element = self.find_element(locator) element.click() def input_text(self, locator, text): element = self.find_element(locator) element.clear() element.send_keys(text) # 其他通用方法... ``` 在这个示例中,`BasePage`类接受一个`driver`参数,该参数是一个已经初始化好的Selenium WebDriver对象。`BasePage`类提供了一些常用的方法,如`open_url`用于打开指定的URL,`find_element`用于查找页面元素,`click`用于点击元素,`input_text`用于输入文本等。 通过封装Selenium基类,你可以在具体的测试页面类中继承`BasePage`类,并直接使用其中定义的方法,从而简化测试代码的编写。例如: ```python class LoginPage(BasePage): def __init__(self, driver): super().__init__(driver) self.username_locator = (By.ID, 'username') self.password_locator = (By.ID, 'password') self.login_button_locator = (By.ID, 'login-button') def login(self, username, password): self.input_text(self.username_locator, username) self.input_text(self.password_locator, password) self.click(self.login_button_locator) # 其他页面特定方法... ``` 在`LoginPage`类中,我们继承了`BasePage`类,并定义了一些页面特定的元素定位器和方法,同时可以直接使用`BasePage`类中定义的通用方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值