2012-04-11 59 views
3

我试图自动执行手动脚本(使用java中的selenium)来检查网页上某个字段(标签:代表必填字段)的粗体外观。什么可以是可能的硒的Java功能,以验证某些元素的大胆外观(在课堂上没有关于外观的信息)如何在硒中验证某个字段的粗体外观

+0

并再次:请添加一些代码。您可以检查包含文本的元素是否有任何负责格式化文本的类。 – faramka 2012-04-11 06:43:39

+0

标签的萤幕检查:我的页面上的货币是

Currency:
它是选择货币的下拉列表 – 2012-04-11 07:30:52

回答

2

可以使用style()方法检查font-weight(假设你实际使用的是硒的webdriver) 。

所以说,你有HTML这样的:

<body> 
    <div id='1' style='font-weight:normal'> 
    <div id='2' style='font-weight:bold'>Field Label</div> 
    <div id='3'>Field</div> 
    </div> 
</body> 

你可以做以下检查字段标签div的字体重量(以下是在Ruby中,虽然同样应该有可能在其他语言)。

el = driver.find_element(:id, "2") 
if el.style('font-weight') >= 700 
    puts 'text is bold' 
else 
    puts 'text is not bold' 
end 
0

我看到你提到的元素具有复合类,为什么不尝试找到所有与label_required类,蟒蛇例如

els = driver.find_elements_by_css_selector(div[class*=label_required]) 
self.assertTrue(len(els) == [known value of required fields on page]) 

的Java(注元素:我不写Java代码,所以语法可能是错误的):

import static org.junit.Assert.assertTrue; 
WebDriver driver = new FirefoxDriver(); 
WebElement els=driver.findElements(By.cssSelector("div[class*=label_required]")); 
assertTrue(els.length == [known value of required fields on page]); 
+0

我正在编写由testng执行的脚本。你可以给我提供java代码(硒) – 2012-04-12 05:55:21

+0

我加入到我的初步评论,请标记为答案,如果这适用于你,谢谢 – 2012-04-13 14:50:12

4

有了webdriver的(在Java中),您可以使用getCssValue()

import static org.junit.Assert.assertTrue; 
(...) 

// assuming elem is a healthy WebElement instance, your found element 
String fontWeight = elem.getCssValue("font-weight"); 
assertTrue(fontWeight.equals("bold") || fontWeight.equals("700")); 

(since 700 is the same as bold)


使用Selenium RC,看到this technique,只需使用(根据使用或fontWeightfont-weight

1

我真的很喜欢贾斯汀·柯的建议使用style("font-weight")方法,但是在Python绑定,等效似乎value_of_css_property("font-weight")

>>> element = self.wd.find_element_by_id("some-id") 
>>> element.value_of_css_property('font-weight') 
u'700' 
>>> element.style('font-weight') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'WebElement' object has no attribute 'style' 

http://code.google.com/p/selenium/source/browse/py/selenium/webdriver/remote/webelement.py#132

道歉,这是一个单独的答案与一对于这个答案的评论,但我显然有太低的业务门槛评论那里