You are on page 1of 3

package tweet;

import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

java.io.File;
java.io.FileInputStream;
java.io.IOException;
java.util.Iterator;
java.util.Map;
java.util.Map.Entry;
java.util.TreeMap;
java.util.concurrent.TimeUnit;
org.apache.poi.ss.usermodel.Cell;
org.apache.poi.ss.usermodel.Row;
org.apache.poi.xssf.usermodel.XSSFSheet;
org.apache.poi.xssf.usermodel.XSSFWorkbook;
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.chrome.ChromeDriver;
org.openqa.selenium.firefox.FirefoxDriver;

public class DataSetTest {


public Map<String, String> getData(String filePath) {
Map<String, String> userMap = new TreeMap<String, String>();
String key = null;
String value = null;
try {
FileInputStream twitFile = new FileInputStream(new File(
filePath));
XSSFWorkbook workbook = new XSSFWorkbook(twitFile);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// --For each row, iterate through all the colum
ns
Iterator<Cell> cellIterator = row.cellIterator()
;
while (cellIterator.hasNext()) {
Cell cell1 = cellIterator.next();
Cell cell2 = cellIterator.next();
key = cell1.getStringCellValue();
value = cell2.getStringCellValue();
// System.out.print(key+"\t\t\t in loop"
);
// System.out.print(value);
if (key != null && value != null)
userMap.put(key, value);
// System.out.print(key.getStringCellVal
ue() + "\t");
// System.out.print(value.getStringCellV
alue() + "\t");
}
}
workbook.close();

twitFile.close();
} catch (Exception e) {
System.out.println("In file Read Exception...");
e.printStackTrace();
}
System.out.println("end of File Read...");
return userMap;
}
public WebDriver openBrowser(String browserName) {
WebDriver driver = null;
if (browserName.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browserName.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "chromedri
ver.exe");
driver = new ChromeDriver();
}
return driver;
}
public String loginTwitter(WebDriver driver,
String userId, String password) {
System.out.println("in loginTwitter....Method");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://twitter.com/");
driver.findElement(By.id("signin-email")).sendKeys(userId);
driver.findElement(By.id("signin-password")).sendKeys(password);
driver.findElement(
By.xpath("//*[@id=\"front-container\"]/div[2]/di
v[2]/form/table/tbody/tr/td[2]/button"))
.click();
String loginSuccess = driver.findElement(By.xpath("/html/body"))
.getAttribute("class");
System.out.println(loginSuccess);
// if (loginSuccess
// .equals("logged-in user-style-amctest8 ms-windows enhanced-mi
ni-profile supports-drag-and-drop"))
// {
System.out.println("Login Successful for .. userId : " + userId)
;
return userId;
}
public void postTweet(WebDriver driver, String userId)
throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"tweet-box-mini-home-profi
le\"]"))
.sendKeys(
"This is post from automation wi
th user ID : " + userId);

driver.findElement(
By.xpath("//*[@id=\"timeline\"]/div[2]/div/form/
div[2]/div[2]/button"))
.click();
Thread.sleep(5000);
}
public void logoutTwitter(WebDriver driver) throws InterruptedException
{
System.out.println("in Logout ....");
driver.findElement(By.xpath("//*[@id=\"user-dropdown-toggle\"]")
)
.click();
driver.findElement(By.xpath("//*[@id=\"signout-button\"]/button"
))
.click();
Thread.sleep(5000);
driver.quit();
System.out.println("Webdriver closed...");
}
public static void main(String[] args) throws IOException,
InterruptedException {
Map<String, String> userMap = new TreeMap<String, String>();
String filePath = "C:/Users/skesanam/twitter/tweet.xlsx";
String browserName = "";
WebDriver driver = null;
String userId = null;
String password = null;
DataSetTest dst = new DataSetTest();
userMap = dst.getData(filePath);
driver = dst.openBrowser(browserName);
for (Entry<String, String> values : userMap.entrySet()) {
userId = values.getKey();
password = values.getValue();
userId = dst.loginTwitter(driver, userId, password);
dst.postTweet(driver, userId);
dst.logoutTwitter(driver);
}
System.out.println("End of prgram execution....");
}
}

You might also like