Professional Documents
Culture Documents
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;
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....");
}
}