本文将分享测试自动化html元素选择器。元素ID或DataAttribute[关闭]的详细内容,并且还将对html元素选择器的写法进行详尽解释,此外,我们还将为大家带来关于appium+python自
本文将分享测试自动化html元素选择器。元素ID或DataAttribute [关闭]的详细内容,并且还将对html元素选择器的写法进行详尽解释,此外,我们还将为大家带来关于appium+python自动化34-获取元素属性get_attribute、appium+python自动化:获取元素属性get_attribute、asp.net – 如何手动调用ValidationAttributes? (DataAnnotations和ModelState)、asp.net-mvc – 在DataAnnotations DataType Attribute中忽略ErrorMessage的相关知识,希望对你有所帮助。
本文目录一览:- 测试自动化html元素选择器。元素ID或DataAttribute [关闭](html元素选择器的写法)
- appium+python自动化34-获取元素属性get_attribute
- appium+python自动化:获取元素属性get_attribute
- asp.net – 如何手动调用ValidationAttributes? (DataAnnotations和ModelState)
- asp.net-mvc – 在DataAnnotations DataType Attribute中忽略ErrorMessage
测试自动化html元素选择器。元素ID或DataAttribute [关闭](html元素选择器的写法)
我目前在UI测试自动化的元素上放置了一些ID。这些ID仅用于测试。我应该添加数据属性,而不是使其对将来的开发人员更具可读性(data-testHandle
=“ mybutton”),还是应该坚持使用ID。
w3.org说:
定制数据属性旨在存储页面或应用程序专用的定制数据,对于这些数据,没有更多合适的属性或元素。
我倾向于保留ID,但是我中的某些人认为将来的开发人员会认为ID未被使用并删除它们。
这里有任何最佳做法。谢谢。
答案1
小编典典这接近于基于意见,这是应该有助于做出选择的总结。
为什么要使用ID
属性:
- 这是一种 常见 的和熟悉的给大家做测试自动化 的方式来定位元素
- 通常,这是在页面上定位元素的 最快 方法,因为硒会降低其执行速度
document.getElementById()
,而硒是由现代浏览器优化的(尽管,端到端UI测试的性能通常并不关键) - 它是每种硒语言绑定中的 内置 定位器
- 如果您要使用Firebug或Chrome开发者工具-CSS选择器和XPath生成工具通常会
id
尽可能使用元素的s 提供更强大的定位器 - 您将构建较短的CSS选择器和XPath表达式。例如
#myid .someclass
反对[automation-id=myid] .someclass
。
为什么要使用自定义属性:
- 例如,如果将
automation-id
属性添加到所有所需的元素,则将在某种程度上对名称空间/作用域进行测试自动化-每个人都将仅从属性名称知道这是什么。意思是,您将大大减少开发人员有意更改属性的可能性,而不是id
属性,属性也可以并且通常也用于应用程序客户端逻辑(请参阅此和此答案)
另外,这是一些相关的线程:
- 使用Selenium时,是否在所有标准实践中都添加了ID?
- 测试中识别元素的能力胜于ID
appium+python自动化34-获取元素属性get_attribute
1.获取text
#定位-书架-签到
e=driver.find_element_by_id("com.ishugui:id/tv_sign_status")
# 获取text
t1 = e.text
print("获取text",t1)
2.tag_name
1.tag_name实质上是获取class属性
# 获取tag_name
t2 = e.tag_name
print(t2)
3.get_attribute
1.获取content-desc属性,这里注意了,如果content-desc属性为空,那么获取的就是text属性,不为空获取的才是content-desc属性
2.content-desc属性为空,打印结果:书架
# content-desc为空,获取的是text
t3 = e.get_attribute("name")
print(t3)
3.content-desc属性不为空,打印结果:百度阅读
# content-desc
t4 = e.get_attribute("name")
print(t4)
备注:content-desc属性也可以这样获取:get_attribute("contentDescription")
4.id,calss,text属性获取
# id
t5 =e.get_attribute("resourceId")
print(t5)
# class
t6 = e.get_attribute("className")
print(t6)
# text
t7 = e.get_attribute("text")
print(t7)
5.其它属性获取,注意这里并不是所有的都可以获取,一些标准的属性是可以获取到的
# checkable
t8 = e.get_attribute("checkable")
print t8
# clickable
t9 = e.get_attribute("clickable")
print t9
4.size和location
1.获取size,返回的是字典,如:{''width'': 84, ''height'': 84}
2.获取location,返回的是字典,如:{''y'': 38, ''x'': 192}
# coding:utf-8
from appium import webdriver
from time import sleep
desired_caps = {
''platformName'': ''Android'',
''deviceName'': ''T8B6W4LJU4VSQWWW'',#127.0.0.1:62001
''platformVersion'': ''6.0'',
''appPackage'': ''com.ishugui'',
''appActivity'': ''com.dzbook.activity.LogoActivity'',
#''noReset'': ''true'',
#''resetKeyboard'': ''true'',
#''unicodeKeyboard'': ''true''
}
driver = webdriver.Remote(''http://127.0.0.1:4723/wd/hub'', desired_caps)
driver.implicitly_wait(20)
driver.wait_activity(".com.dzbook.activity.LogoActivity", 10)
#进入主界面
#定位-书架-签到
e=driver.find_element_by_id("com.ishugui:id/tv_sign_status")
# 获取text
t1 = e.text
print("获取text",t1)
# 获取tag_name
t2 = e.tag_name
print(t2)
# content-desc为空,获取的是text
t3 = e.get_attribute("name")
print(t3)
# content-desc
t4 = e.get_attribute("name")
print(t4)
# id
t5 = e.get_attribute("resourceId")
print(t5)
# class
t6 = e.get_attribute("className")
print(t6)
# text
t7 = e.get_attribute("text")
print(t7)
# checkable
t8 = e.get_attribute("checkable")
print(t8)
# clickable
t9 = e.get_attribute("clickable")
print(t9)
# size
t10 = e.size
print(t10)
# location
t11 = e.location
print(t11)
appium+python自动化:获取元素属性get_attribute
使用get_attribute()获取元素属性,括号里应该填写什么?
查看appium源码
self.driver.find_element(MobileBy.XPATH, "//*[contains(@resource-id,'followed_btn')]").get_attribute('resourceId')
asp.net – 如何手动调用ValidationAttributes? (DataAnnotations和ModelState)
目前,我基本上遍历所有ValidationAttribute实例中的每个属性加载并尝试使用Validate / IsValid函数进行验证,但这似乎对我没有用.
作为一个例子,我有一个模型,如:
public class HobbyModel { [required(AllowEmptyStrings = false,ErrorMessage = "Do not allow empty strings")] [displayName("Hobby")] [DataType(DataType.Text)] public string Hobby { get; set; } }
检查属性的代码是:
object[] attributes = propertyInfo.GetCustomAttributes(true); TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(ValidationAttribute)); bool isValid = false; foreach (object attr in attributes) { ValidationAttribute attrib = attr as ValidationAttribute; if (attrib != null) { attrib.Validate(obj,propertyInfo.Name); } }
我调试了代码,模型确实有3个属性,其中2个是从ValidationAttribute派生的,但是当代码通过Validate函数(带有空值或空值)时,它会按预期抛出异常.
我期待我做一些愚蠢的事情,所以我想知道是否有人使用过这个功能并且可以提供帮助.
提前致谢,
杰米
解决方法
Validate
方法,而不是属性值.以下更有可能按预期工作(尽管显然不适用于索引属性):
attrib.Validate(propertyInfo.GetValue(obj,null),propertyInfo.Name);
不过,你肯定会更容易using the Validator class作为Steven suggested.
asp.net-mvc – 在DataAnnotations DataType Attribute中忽略ErrorMessage
public class Appointment { [required(ErrorMessage="Please enter your name")] public string Name { get; set; } [required(ErrorMessage="Please enter your appointment date?")] [DataType(DataType.Date,ErrorMessage="Appointment date is not a date")] public DateTime AppointmentDate { get; set; } }
“required”属性遵循ErrorMessage中的值;也就是说,如果我没有输入值,我会收到“请输入”消息.但是,如果我在DateTime字段中输入一个字符串,我收到标准系统错误消息“值’blah’对AppointmentDate无效”.
我通过ASP.NET MVC代码调试,似乎在FormatException的情况下,它没有从propertyMetadata中选择正确的显示名称.不管怎样,或者我遗漏了一些明显的东西:/
有人遇到过这个问题吗?是我,还是只是beta(我使用的是ASP.NET MVC 2 Beta)?
解决方法
这是项目的一小段摘录(使用DataAnnotations和xVal):
private List<ErrorInfo> _errors; private List<ErrorInfo> Errors { get { if (_errors == null) _errors = new List<ErrorInfo>(); return _errors; } //set { _errors = value; } } private string _startDateTimeString; /// <summary> /// A string reprsentation of the StartDateTime,used for validation purposes in the views. /// </summary> /// <remarks> /// If the user passes something like 45/45/80 it would be a valid mm/dd/yy format,but an invalid date,/// which would cause an InvalidOperationException to be thrown by UpdateModel(). By assigning dates to string properties /// we can check not only the format,but the actual validity of dates. /// </remarks> public string StartDateTimeString { get { return _startDateTimeString; } set { // Check whether the start date passed from the controller is a valid date. DateTime startDateTime; bool validStartDate = DateTime.TryParse(value,out startDateTime); if (validStartDate) { StartDateTime = startDateTime; } else { Errors.Add(new ErrorInfo("StartDateTime","Provide a valid date for the start time.")); } _startDateTimeString = value; } } partial void OnValidate(ChangeAction action) { if (action != ChangeAction.Delete) { Errors.AddRange(DataAnnotationsValidationRunner.GetErrors(this)); if (StartDateTimeString != null) { DateTime startDateTime; if (!DateTime.TryParse(StartDateTimeString,out startDateTime)) { Errors.Add(new ErrorInfo("StartDateTime","Provide a valid date for the start time.")); } } if (Errors.Any()) throw new RulesException(Errors); } } }
在我们项目的两个地方进行检查是有意义的,但我只想向您展示一个概念.
我们今天的关于测试自动化html元素选择器。元素ID或DataAttribute [关闭]和html元素选择器的写法的分享已经告一段落,感谢您的关注,如果您想了解更多关于appium+python自动化34-获取元素属性get_attribute、appium+python自动化:获取元素属性get_attribute、asp.net – 如何手动调用ValidationAttributes? (DataAnnotations和ModelState)、asp.net-mvc – 在DataAnnotations DataType Attribute中忽略ErrorMessage的相关信息,请在本站查询。
本文标签: