Thursday 20 June 2013

Handling Windows Processes while running Selenium WebDriver script

 In general we use one step to kill the windows process:

             WindowsUtils.tryToKillByName("ProcessName.exe");

Now I am pasting here my code of killing skype.exe process before running webdriver script

package WDTNG;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.os.WindowsUtils;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class HandleWP {
    WebDriver d;
    @BeforeMethod
    public void start() throws Exception
    {
       
            WindowsUtils.tryToKillByName("skype.exe");
            d = new FirefoxDriver();
    }
    @Test
    public void test()
    {
        d.get("http://www.google.com");
    }
    @AfterMethod
    public void am()
    {
        d.quit();
    }
}

MySQL Replication

MySQL Replication
MySQL is an excellent open source database system. Replication is a great way to keep data redundant in case of a server crash. However, replication should not take the place of backups in case of data corruption or miss-entered data – as this data will also be replicated to the slave.
MySQL replication takes place in a master-slave configuration. Be aware that by using the configuration – only changes made on the master are replicated to the slave. Any changes on the slave will not be replicated to the master.
Following the steps below, you can have MySQL replication setup.
Master Server
1. Open the my.ini (C:\programdata\MySQL) file.
2. Enter somewhere below ‘[MySQLd]‘ on the Master Server.

log-bin=MySQL-bin
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1

3. Restart MySQL service (services.msc) on the Master Server

4. Create a user on the master with the ‘REPLICATION SLAVE’ privilege. This user needs no other privileges.
Replace X.X.X.X with the IP address of the Slave Server.
CREATE USER ‘user’@ ‘X.X.X.X’ IDENTIFIED BY ‘password’;
GRANT REPLICATION SLAVE ON *.* TO ‘user’@'X.X.X.X’ IDENTIFIED BY ‘password’;

5. Execute ‘FLUSH TABLES WITH READ LOCK;’ on the master to prevent writing to the databases.
6. Execute ‘SHOW MASTER STATUS;’ on the master and record the values for later.
7. Execute ‘UNLOCK TABLES;’ on the master.
Slave Server
1. Open the my.ini on the Slave Server.
2. Enter somewhere below ‘[MySQLd]‘ on the Slave Server:
server-id=2
3. Save the file and restart MySQL service.
4. Execute the following on the Slave Server (adjust values accordingly to user setup in step 4 and values retrieved from step 6 in Master Server):
CHANGE MASTER TO
MASTER_HOST=’X.X.X.X’,
MASTER_USER=’user’,
MASTER_PASSWORD=’password’,
MASTER_PORT=3306,
MASTER_LOG_FILE=’MySQL-bin.000001′,
MASTER_LOG_POS=98,
MASTER_CONNECT_RETRY=10;

5. Execute the following on the Slave Server:
START SLAVE;
6. Check the MySQL log on the slave to ensure that the connection to the master has been successful. You should see a line similar to the following:
080609 8:47:02 [Note] Slave I/O thread: connected to master ‘root@X.X.X.X:3306′, replication started in log ‘MySQL-bin.000001′ at position 98
You should now have a successful MySQL Master-Slave configuration.
If you have any questions please let me know.

Sunday 16 June 2013

Login page validation messages verification using DataDriven

I have verified these 5 testcases using the below script :

1.Enter correct username(cuname) and wrong password(wpwd)
2.Enter wrong username(wuname) and correct password(cpwd)
3.Test with empty username and correct password
4.Test with  correct username and empty password
5.Enter correct username(cuname) and correct password(cpwd)

Prerequisite:
   Add jxl jar into project
   Install TestNG
   Add WebDriver package

package WDTNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

import jxl.Workbook;
import jxl.Sheet;

public class LoginDD {
   
WebDriver d;
@BeforeMethod
    public void BM()
    {
    d = new FirefoxDriver();
    d.get("http://gmail.com");
    }

@Test
    public void T()
    {
    try{
        d.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        Sheet s;
        FileInputStream fi = new FileInputStream("C:\\Users\\Kalyan\\Desktop\\S2TNG\\LoginPage.xls");
        Workbook w = Workbook.getWorkbook(fi);
        s = w.getSheet(1);
   
        for(int row=1; row <=s.getRows();row++)
        {
            String username = s.getCell(0, row).getContents();
            String password= s.getCell(1, row).getContents();
            d.findElement(By.xpath(".//*[@id='Email']")).clear();
            d.findElement(By.xpath(".//*[@id='Email']")).sendKeys(username);
       
            d.findElement(By.xpath(".//*[@id='Passwd']")).clear();
            d.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys(password);
            d.findElement(By.xpath(".//*[@id='signIn']")).click();
            try
            {
                if(waitforelement(By.xpath(".//*[@id='errormsg_0_Email']")))
                {
                    WebElement element = d.findElement(By.xpath(".//*[@id='errormsg_0_Email']"));
                    String strng = element.getText();
                    System.out.println(strng);
                }
       
                else if(waitforelement(By.xpath(".//*[@id='errormsg_0_Passwd']")))
                {
                    WebElement element = d.findElement(By.xpath(".//*[@id='errormsg_0_Passwd']"));
                    String strng = element.getText();
                    System.out.println(strng);
                    //Assert.assertEquals("Google Search", strng);
                }
                else if(waitforelement(By.xpath(".//*[@id='errormsg_0_Passwd']")))
                {
                    WebElement element = d.findElement(By.xpath(".//*[@id='errormsg_0_Passwd']"));
                    String strng = element.getText();
                    System.out.println(strng);
                }
                else if(waitforelement(By.xpath(".//*[@id=':b7']/div/div")))
                {
                    WebElement element = d.findElement(By.xpath(".//*[@id=':b7']/div/div"));
                    String strng = element.getText();
                    System.out.println(strng);
                }
                else
                {
                    System.out.println("Script has failed");
                    d.quit();
                }
            }
            catch(Exception e)
            {
               
                //e.printStackTrace();
                //throw e;
            }
        }
   
       
    }
    catch (Exception e)
    {
       
    }
       
}
   
public boolean waitforelement(By name)
{
       
    try
    {
        if(d.findElement(name).isDisplayed())
        {
            return true;
        }
    }
       
    catch(Exception e)
    {
        return false;
    }
    return false;
       
}


@AfterMethod
public void AM()
{
    d.quit();
}
}


See this below image and create an excel sheet with the following data 
Note:Save the excel sheet as fully compatible with Excel 97-2003






Saturday 1 June 2013

SELENIUM 2(WebDriver) with GRID

Prerequisites:
     1.Install TestNG in Eclipse using the URL:http://beust.com/eclipse
     2.Download WebDriver package from the location link
     3.Download Selenium-server jar file from the location link

I have created 2 sample scripts for demo :

sample.java
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class sample {

WebDriver d;
@BeforeMethod
public void BM() throws MalformedURLException
{
DesiredCapabilities capability=null;
capability= DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
d = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
d.get("http://google.com");
}
@Test
public void test()
{
System.out.print("test");
}
@AfterMethod
public void AM()
{
d.quit();
}
}

sample1.java
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class sample1 {

WebDriver d;
@BeforeMethod
     public void BM() throws MalformedURLException
{
DesiredCapabilities capability=null;
capability= DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
d = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
d.get("http://google.com");
}
@Test
    public void test()
{
System.out.print("test");
}
@AfterMethod
public void AM()
{
d.quit();
}
}

TestNG.XML

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Selenium Grid with webdriver" verbose="3"  parallel="classes" thread-count="2">  
  <test name="Selenium Grid Demo">
  <parameter name="browser" value="firefox"/>
    <classes>
      <class name="packagename.sample"/>
 <class name="packagename.sample1"/>
    </classes>
 </test>

 </suite>

Now to start the Grid and Node: open CMD and navigate to selenium-server-standalone-x.x.x.jar file is located and use the following commands

Create hub using below command
java -jar selenium-server-standalone-2.33.0.jar -role hub

Create node using below command
java -jar selenium-server-standalone-2.33.0.jar -role webdriver -hub http://localhost:4444/grid/register

Create another node using below command
java -jar selenium-server-standalone-2.33.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556


Note:You can create N number of nodes,but give different port number for each node