Friday, May 27, 2011

Switching between frames and WebDriver

I have web page which has two frames -navigator and content.I have to click on a link in the navigator and the content of the frame 'content' changes.Now I have to input some value in the content frame.I use IDE  to record and generate code in the junit4 webdriver format.
        selenium.selectFrame("navigator");
        selenium.click("link=mylink");
        selenium.selectFrame("relative=up");
        selenium.selectFrame("content");
        selenium.type("myinputfield", "7");

Now when I try to run this code I get error at relative=up.So I google and find that this is not the way to write webdriver code.I rewrote the entire code in the webdriver style coding .So now I have this code.
        driver.switchTo().frame("navigator");
        WebElement my_nav_link = driver.findElement(By.partialLinkText("MyLinkText"));
        my_nav_link.click();
        driver.switchTo().frame("content");
        WebElement myinputfieldelem= driver.findElement(By.id("myinputfield"));
         myinputfieldelem.sendKeys("7");
Now when I try again , it errors out at frame not found for content .
So I try to debug a bit and I add the below code just before switch to frame to see all the frames present.
        List<WebElement> frameset=driver.findElements(By.tagName("frame"));
        if(frameset.size()>0)
        {
            for (WebElement framename :frameset)
            {
                System.out.println("frame id:" + framename.getAttribute("name"));
            }
        }
There are no frames ,so that means I am now in navigator frame and I have to go one level up to find the frame content.
So I tried 
         driver.switchTo().frame("relative=up");
This also failed.Now I am clueless and and could  not figure  out what to do.After some googling i found out another option which saved me  and the final code which works is as below.
         driver.switchTo().frame("navigator");
        WebElement my_nav_link = driver.findElement(By.partialLinkText("MyLinkText"));
        my_nav_link.click();
        driver.switchTo().defaultContent();
        driver.switchTo().frame("content");
        WebElement myinputfieldelem= driver.findElement(By.id("myinputfield"));
        myinputfieldelem.sendKeys("7");
 So the outcome is you have to use  switchTo().defaultContent() to be able to come out of a frame and go to a sibling frame.

Wednesday, May 25, 2011

Selenium First steps

I am in the process of evaluating using selenium for automating a web interface.So first of all I don't know anything about it and I am trying o figure out how things work in selenium.
I first installed the selenium ide as a plugin to firefox from here.
I recorded a sample script and did a play back.Yes my first test worked like a charm .
Now I see there code and it is in html.I found out that there is way to generate code in Junit4 format (Options->Format->Junit 4).I changed the format to Junit 4.Now the play back option is gone.
So how do I play back Junit4 test cases of selenium??
Then I see there is a selenium server.Now the whole confusion starts and I know these are starting roadblocks.
There is selenium2 as well as selenium 1 and there is something called webdriver also.So I thought I will install the latest and I installed the selenium 2 server .I started it from console and I thought I will be able to run junit tests from there.But once I started server from console I realized ,there is no option to start scripts from there. Now I find something called as java driver and there is some firefox driver. I was confused as to what to use where.Later I found that java driver is used along with eclipse.So followed the steps to use the java driver in eclipse.OK, now I record the scripts in selenium ide in junit format and copy paste that code to eclipse and from here I can run the junit test cases.

So I am happy with the setup.But When I look at the test cases which has SeleneseTestCase is reported as deprecated by Eclipse.So What should I Do?Now I google again and I find that there is a webdriver format supported IDE plugin for firefox which  get from here. After installing the plugin I could generate code in Junit4 webdriver format and there is no warnings.