Thursday

Selenium - Designing First Test case in Selenium

After installing Eclipse , testng plug-in , Selenium RC server. I am starting with creating first testcase Login and Logout

1. OPen Mozilla
2. Tool > Selenium IDE
3. Record the Script
4. Stop recording
5. Export The file as Testng from File > Export
6. Now open Eclipse ( which having testng , EC server running)
7. Create a Project type JAVA
and Create a new class name Log_inOut init.

8. paste the recorded script in that:

package com.example.tests;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.util.regex.Pattern;

public class Log_inOut extends SeleneseTestNgHelper {
@Test public void testLog_inOut() throws Exception {
selenium.open("/web/portal/login?TYPE=33554433&REALMOID=06-0003637f-cceb-1ba3-8e98-fa5682c8c044&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-Lv3PYArtpFEOxfQZFPHmNauhn5NYebq9auIeK%2fNS2KxEB3P9bprxh3XDZlbLhNS47%2fKK00%2fHVfFG6UHhEfNWMITnNbU4XRY1&TARGET=-SM-http%3a%2f%2frwcrh8%2eca%2ecom%2f");
selenium.type("_58_password", "test");
selenium.click("//input[@value='Sign In']");
selenium.waitForPageToLoad("30000");
selenium.click("link=Sign Out");
}
}

9. Now remove
package com.example.tests;
extends SeleneseTestNgHelper
throws Exception

So It becomes:

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
//import java.util.regex.Pattern;

public class Log_inOut {
@Test public void testLog_inOut(){
selenium.open("/web/portal/login?TYPE=33554433&REALMOID=06-0003637f-cceb-1ba3-8e98-fa5682c8c044&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-Lv3PYArtpFEOxfQZFPHmNauhn5NYebq9auIeK%2fNS2KxEB3P9bprxh3XDZlbLhNS47%2fKK00%2fHVfFG6UHhEfNWMITnNbU4XRY1&TARGET=-SM-http%3a%2f%2frwcrh8%2eca%2ecom%2f");
selenium.type("_58_password", "test");
selenium.click("//input[@value='Sign In']");
selenium.waitForPageToLoad("30000");
selenium.click("link=Sign Out");
}
}


10. Save
11. Run the class As testng.

12. ITS FAILED :) and now I spend 2-3 hour to understand what happened:

13. First of all a) there is no browser mentioned b)there is no refernce of selenium
public Selenium selenium; after the class declaration.

so I added piece of code

import com.thoughtworks.selenium.*;import org.testng.annotations.*;
public class Log_inOut {

public Selenium selenium;

@BeforeClass
public void setup()
{
selenium = new DefaultSelenium("localhost", 4444, "*iexplore","http://rwcrh8.ca.com/");
selenium.start();

}

it will tell IE to open the following url

14. Then I also wrote @Aftertest which will close Selenium

@AfterClass
public void StopTest()
{
selenium.stop();
}

15. Now I run the test and IT AGAIN FAILED ..... I found that my url is redirecting to https and Selenium is unable to pick the whole string

16. so in @Test section I gave full URL
selenium.open("https://rwcrh8.ca.com//web/portal/login?TYPE=33554433&REALMOID=06-0003637f-cceb-1ba3-8e98-fa5682c8c044&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-Lv3PYArtpFEOxfQZFPHmNauhn5NYebq9auIeK%2fNS2KxEB3P9bprxh3XDZlbLhNS47%2fKK00%2fHVfFG6UHhEfNWMITnNbU4XRY1&TARGET=-SM-http%3a%2f%2frwcrh8%2eca%2ecom%2f");

17. but some time it run some time it fails. I found that page loading is creating problem

so I put selenium.waitForPageToLoad("30000"); whenever I am feeling the page load is taking time. But be sure that it is not inbetween the two input field like

selenium.type("_58_login", "test@ca.com");
//selenium.waitForPageToLoad("10000"); For this I got error and It took time to get that if it is not there than I am able to fill both of fields.

selenium.type("_58_password", "test");

18. And I ran script . IT AGAIN FAILED
I realize that I am getting a IE error that page is unsafe and do you want to proceed ....

IN Ineternet explorer I did Tool > devlopment and get the ID of that link and clicked it
in FF I use Firebug to get the ID of that .and add this code in script.

selenium.click("Id=overridelink");


19. Now I ran and YEAH IT RAN


So here is my first script :-)


//package com.example.tests;

import com.thoughtworks.selenium.*;

import org.testng.annotations.*;

public class Log_inOut {

public Selenium selenium;

@BeforeClass
public void setup()
{
selenium = new DefaultSelenium("localhost", 4444, "*iexplore","http://rwcrh8.ca.com/");
selenium.start();

}

@Test
public void testLog_inOut() {



selenium.open("https://rwcrh8.ca.com//web/portal/login?TYPE=33554433&REALMOID=06-0003637f-cceb-1ba3-8e98-fa5682c8c044&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-Lv3PYArtpFEOxfQZFPHmNauhn5NYebq9auIeK%2fNS2KxEB3P9bprxh3XDZlbLhNS47%2fKK00%2fHVfFG6UHhEfNWMITnNbU4XRY1&TARGET=-SM-http%3a%2f%2frwcrh8%2eca%2ecom%2f");
selenium.waitForPageToLoad("30000");

//< id="overridelink" href="http://rwcrh8.ca.com/web/portal/login?TYPE=33554433&REALMOID=06-0003637f-cceb-1ba3-8e98-fa5682c8c044&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-Lv3PYArtpFEOxfQZFPHmNauhn5NYebq9auIeK%2fNS2KxEB3P9bprxh3XDZlbLhNS47%2fKK00%2fHVfFG6UHhEfNWMITnNbU4XRY1&TARGET=-SM-http%3a%2f%2frwcrh8%2eca%2ecom%2f">
selenium.click("Id=overridelink");
//selenium.open("/web/portal/login?TYPE=33554433&REALMOID=06-0003637f-cceb-1ba3-8e98-fa5682c8c044&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-Lv3PYArtpFEOxfQZFPHmNauhn5NYebq9auIeK%2fNS2KxEB3P9bprxh3XDZlbLhNS47%2fKK00%2fHVfFG6UHhEfNWMITnNbU4XRY1&TARGET=-SM-http%3a%2f%2frwcrh8%2eca%2ecom%2f");

selenium.waitForPageToLoad("30000");

selenium.type("_58_login", "test@ca.com");
//selenium.waitForPageToLoad("10000");

selenium.type("_58_password", "test");
selenium.click("//input[@value='Sign In']");
selenium.waitForPageToLoad("40000");
selenium.click("link=Sign Out");

// selenium.waitForPageToLoad("10000");


}

@AfterClass
public void StopTest()
{
selenium.stop();
}

}


20 . Now I created a package in eclipse and put 2 classes (same cut copy paste of first for second) and ran.
Suite ran successfully.

Now next post is to build basic Automation framework....

No comments:

Post a Comment