Saturday, May 10, 2014

Login Logout scenario for yahoo.com

Here is the code to login and logout in yahoo.com. Here its fetching the userid and password from excel sheet.

import java.awt.AWTException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class YahooLoginLogOut {
    public static void main(String[] args) throws AWTException, InterruptedException, InvalidFormatException, IOException {
    //get the data from excel sheet
            FileInputStream fis = new FileInputStream("./InputData.xlsx");
            Workbook wb = WorkbookFactory.create(fis);
            String emailId = wb.getSheet("Sheet2").getRow(0).getCell(0).getStringCellValue();
            String password = wb.getSheet("Sheet2").getRow(1).getCell(0).getStringCellValue();
           
            //open browser and Yahoo
            WebDriver driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://in.yahoo.com/");
            Actions act = new Actions(driver);
            //login
            act.moveToElement(driver.findElement(By.xpath("//em[text()='Sign In']"))).perform();
            driver.findElement(By.xpath("//span//a[@class='login-svc ylogin login-btn-purple rapid-noclick-resp login-btn-small']")).click();
            driver.findElement(By.id("username")).sendKeys(emailId);
            driver.findElement(By.id("passwd")).sendKeys(password);
            driver.findElement(By.xpath("//button[contains(text(),'Sign in')]")).click();
           
            //sign out
            act.moveToElement(driver.findElement(By.xpath("//img[contains(@class,'tab-icon')]"))).perform();
            driver.findElement(By.xpath("//span[text()='Sign Out']")).click();
           
            driver.close();
    }
}

2 comments:

  1. Hi Sanjay Kumar, you have done a good stuff for selenium starters like me :) . could you please convert the above script to support jxl.

    ReplyDelete
  2. can you tell me about "Vector smash protection is enabled." which comes in console.

    ReplyDelete