A few months back I wrote a simple script for facebook which helps you to quickly post status updates. Similarly, in this post, we will try to achieve the same with Twitter.

Steps covered:
1. Launch Twitter
2. Login using valid Credentials
3. On the Home page, click on the tweet box
4. Write the Tweet and Post-it
5. Take a Screenshot
6. Logout
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.testersdock.testcase;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.io.Files;

public class TwitterLogin {

public static void main(String[] args) throws InterruptedException, IOException {

//Creating reference of Webdriver Interface
WebDriver driver;

//Declare Twitter Credentials
String user="";
String pass="";

//Creating an instance of chrome
ChromeOptions coptions = new ChromeOptions();

// Telling Selenium to find Chrome Driver
System.setProperty("webdriver.chrome.driver", "/Users/abc/Desktop/chromedriver");

// Initialize browser
driver = new ChromeDriver(coptions);

// Launch Twitter
driver.get("http://twitter.com/login");

//Maximize Window
driver.manage().window().maximize();

//Wait Till Username Field is displayed
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[@placeholder='Phone, email or username']")));

//Enter Username
WebElement userTextField = driver.findElement(By.xpath(".//input[@placeholder='Phone, email or username']"));
userTextField.sendKeys(user);

//Enter Password
WebElement PassTextField = driver.findElement(By.xpath(".//input[@class='js-password-field']"));
PassTextField.sendKeys(pass);

//Click on Login button
driver.findElement(By.xpath(".//button[@type='submit']")).click();

//Wait
Thread.sleep(3000);

//Find the Tweet Box and enter the Tweet Hello World
WebElement tweetBox = driver.findElement(By.id("tweet-box-home-timeline"));
tweetBox.click();
tweetBox.sendKeys("Hello World");

//Click on Tweet Button
driver.findElement(By.xpath(".//button[@class='tweet-action EdgeButton EdgeButton--primary js-tweet-btn']")).click();

//Wait
Thread.sleep(2000);

// Take Screenshot for Evidence
File srce = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

// Save the screenshot in the given path by the name Tweet.png
Files.copy(srce, new File("/Users/abc/Desktop/Screenshots/Tweet.png"));

//Click on Profile and Settings
driver.findElement(By.id("user-dropdown-toggle")).click();

//Wait and Click Logout Button
WebElement logout = wait.until(ExpectedConditions.elementToBeClickable(By.id("signout-button")));
logout.click();

//Wait
Thread.sleep(2000);

// Close the browser
driver.quit();
}
}