Re: How do I verify a drop down value has an option value attribute of "selected" [webdriver with java]

4,444 views
Skip to first unread message

fmtjatt

unread,
Dec 12, 2012, 3:34:59 PM12/12/12
to webd...@googlegroups.com
You can use .getAttribute to get the value. Try the follow

String isSelected = driver.findElement(By.id("fontList")).getAttribute(selected);
if(isSelected .contains("selected"){
    reporter.log("pass");
}else{
    reporter.log("fail");
}

On Wednesday, December 12, 2012 7:45:41 AM UTC-8, webdriver simon wrote:
Hi All,

Hopefully someone can help point me in the right direction.  Really pulling my hair out over this and it should be so simple - Just can't figure it out.

My application has a font list drop down menu.  When I select a font from the menu, the option value for the respective font types will have a "selected=selected" attribute applied to it.  What I want to do, is select each font option from the menu (which I have figured out already) and then verify that each option value contains the "selected=selected" attribute (so I know it's been set properly)

Here is my HTML code:

<select id="fontList" name="fontList">
    <option value="Arial, Helvetica, sans-serif">Sans-Serif</option>
    <option value="Times New Roman, Times, serif" selected="selected">Serif</option>
    <option value="Courier New, monospace">Monospace</option>
    <option value="Trebuchet MS, Trebuchet, sans-serif">Trebuchet</option>
    <option value="Verdana, Geneva, sans-serif">Verdana / Geneva</option>
    <option value="Comic Sans MS, Comic Sans, cursive">Comic Sans</option>
</select>

Many thanks for your help

Amit Agarwal

unread,
Dec 13, 2012, 4:32:16 AM12/13/12
to webd...@googlegroups.com
Hi,

can you please little bit give more explanation of the objective of your testing, lets see if I understood your issue

1. Select dropdown using SELECT 
2. Identify all elements of the dropdown ( You can write a method, which will return content of your dropdown)
3. Select any specific element of the dropdown ( how you are doing this, using index or text ?)
4. Wait for some time, as entire page is suppose to re-render, make sure that you will be getting a new DOM here.
5. Again locate your color dropDown.
6. Now check each element, using 
              element.isSelected(), and check which element ( index , value) is checked

7. Now compare <Step 3> and Step 6, if you are getting same element.

we can work on code also if you confirm mine understanding.

Amit



On Wed, Dec 12, 2012 at 9:15 PM, webdriver simon <buckfa...@googlemail.com> wrote:
Hi All,

Hopefully someone can help point me in the right direction.  Really pulling my hair out over this and it should be so simple - Just can't figure it out.

My application has a font list drop down menu.  When I select a font from the menu, the option value for the respective font types will have a "selected=selected" attribute applied to it.  What I want to do, is select each font option from the menu (which I have figured out already) and then verify that each option value contains the "selected=selected" attribute (so I know it's been set properly)

Here is my HTML code:

<select id="fontList" name="fontList">
    <option value="Arial, Helvetica, sans-serif">Sans-Serif</option>
    <option value="Times New Roman, Times, serif" selected="selected">Serif</option>
    <option value="Courier New, monospace">Monospace</option>
    <option value="Trebuchet MS, Trebuchet, sans-serif">Trebuchet</option>
    <option value="Verdana, Geneva, sans-serif">Verdana / Geneva</option>
    <option value="Comic Sans MS, Comic Sans, cursive">Comic Sans</option>
</select>

Many thanks for your help

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/eim-inbchb4J.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Abhi

unread,
Dec 13, 2012, 4:58:56 AM12/13/12
to webd...@googlegroups.com
Hi webdriver simon,

Use bellow code ... it will get selected option label name in String ...  u need to only pass the ID of the drop down

String selectedLabel = new Select(webDriver.findElement(By.id(id))).getFirstSelectedOption().getText();


Abhishek

webdriver simon

unread,
Dec 17, 2012, 11:45:26 AM12/17/12
to webd...@googlegroups.com
Hi guys,

Thank you for your replies.  I will test out your suggestions and let you know how I get on

Thank you again

Much appreciated  :)

webdriver simon

unread,
Dec 17, 2012, 12:22:18 PM12/17/12
to webd...@googlegroups.com
* Disclaimer *
Couldn't get your suggestions to work.  I'm not sure if the "selected" method is depreictaed but I just don't have an option for it.  I realise this is likely a nasty way of doing what I want and it will demonstrate my lack of Java programming knowledge.  In fact, I have about 7 days programming experience  :)

I created a new function as follows:

  // function to check on each available font and verify it is set in the style attribute of the html code

    public void availfonts(final String availfontset, String availfontget)throws Exception{

        WebElement fonttype = driver.findElement(By.id("fontTab"))  ;
        fonttype.click();
        Thread.sleep(3000);
        // select font size
        Select select = new Select(driver.findElement(By.id("fontList")));
        select.selectByVisibleText(availfontset);
        // verify font size
        WebElement fontType = driver.findElement(By.id("pagebody"))  ;
        String verifiedfont = fontType.getAttribute("style")  ;
        if (verifiedfont.contains(availfontget))  {
        }   else{
            throw new Exception("font type not correct")  ;
        }

    }

And code as follows:

        //Check all available fonts
        System.out.println("Checking fonts");
        availfonts("Sans-Serif", "font-family: Arial, Helvetica, sans-serif;")  ;
        Thread.sleep(2000);
        availfonts("Serif", "font-family: 'Times New Roman', Times, serif;")  ;
        Thread.sleep(2000);
        availfonts("Monospace", "font-family: 'Courier New', monospace;")  ;
        Thread.sleep(2000);
        availfonts("Trebuchet", "font-family: 'Trebuchet MS', Trebuchet, sans-serif;")  ;
        Thread.sleep(2000);
        availfonts("Verdana / Geneva", "font-family: Verdana, Geneva, sans-serif;")  ;
        Thread.sleep(2000);
        availfonts("Comic Sans", "font-family: 'Comic Sans MS', 'Comic Sans', cursive;")  ;
        Thread.sleep(2000);


Any pointers for a better way or for a way to keep my code clean and easier to maintain would be much appreciated. 

Mike Riley

unread,
Dec 17, 2012, 5:12:53 PM12/17/12
to webd...@googlegroups.com
For someone that has only been programming for 7 days this isn't too bad.  At least you are commenting things fairly well, which most newbies do not do.

I personally use: select.selectByValue(valueString);

So I haven't used selectByVisibleText().  I wonder if you need to click on the select element to make the list visible for that to work or not, but I am guessing it should work as you are trying to use it here.

One thing you need to do is remove your sleep() calls.  You are wasting 30 minutes here in sleeps just for a single test!

Either enable implicit waits or use a routine to verify the element is present (I use the latter method, so I can change the time to wait).

You also have a weird if-else:
        if (verifiedfont.contains(availfontget))  {
        }   else{
            throw new Exception("font type not correct")  ;
        }

This should be:
        if (!verifiedfont.contains(availfontget))  {
            throw new Exception("font type not correct")  ;
        }

I had an instructor that said never to use the not operator (!) in an if, but that is a load of ....

I would also do a find of the select in your calling routine, so you only do it once, and pass it to the availfonts method.  Otherwise you do a whole new search every time.  You don't need to do that unless your select in the routine causes the original one to go stale by changing the page.

Mike

webdriver simon

unread,
Jan 2, 2013, 9:56:39 AM1/2/13
to webd...@googlegroups.com
Hi Mike,

Sorry I'm only getting back to you now.  Thank you for taking the time out to help me and pointing me in the right direction.  Very much appreciated.

Happy new year!
Reply all
Reply to author
Forward
0 new messages