Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify bold appearance of a certain field in selenium

Tags:

java

selenium

I am trying to automate a manual script (using selenium in java) to check the bold appearance of a certain field(label:which stands for mandatory field) on a web page . what can be the possible selenium java functions to verify the bold appearance of certain element(In class there is no information about the appearance)

like image 567
Virendra Joshi Avatar asked Apr 11 '12 05:04

Virendra Joshi


People also ask

How do you check bold text in Selenium?

With WebDriver (in Java), you can use getCssValue(). With Selenium RC, see this technique, just use font-weight (or fontWeight depending on the usage). Save this answer.

How do you check the color of an element in Selenium?

We can verify the color of a webelement in Selenium webdriver using the getCssValue method and then pass color as a parameter to it. This returnsthe color in rgba() format. Next, we have to use the class Color to convert the rgba() format to Hex.


2 Answers

With WebDriver (in Java), you can use 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)


With Selenium RC, see this technique, just use font-weight (or fontWeight depending on the usage).

like image 132
Petr Janeček Avatar answered Sep 27 '22 20:09

Petr Janeček


You can check the font-weight using the style() method (assuming you are actually using Selenium-Webdriver).

So say you have HTML like:

<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>

You can do the following to check the font-weight of the field label div (the following is in Ruby, though similar should be possible in the other languages).

el = driver.find_element(:id, "2")
if el.style('font-weight') >= 700
  puts 'text is bold'
else
  puts 'text is not bold'
end 
like image 42
Justin Ko Avatar answered Sep 27 '22 19:09

Justin Ko