In the previous post, we had discussed about the installation of Java, and configuration of Eclipse with selenium WebDriver. Now here, we will be creating a small Java script. All beginners will first want to open browser and to automate it. So here, we will be doing that.
First of all, we will write the scenario of what we will be doing here. Here, we will Login to Gmail account and will automate the below scenarios.
- Open a Firefox browser
- Navigate to the URL
- Maximize the window
- Enter the User Name and Password
- Sign-In to the Gmail Account
- Click on the Compose Button
- Sign-Out from the gmail account
- Close the browser
Now we will automate the above scenario. In the previous post, I had discussed about creating a Java Project, Package, Class. And how to import the JAR files into the project. Here also, we have followed the same thing. For example, we will create a Project called “Gmail
” , Package as “login
”, Class as “Login1
″. Now, we will write the code as below:
package login;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Login1 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://mail.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("Email")).sendKeys(" YOUR USER NAME");
driver.findElement(By.id("Passwd")).sendKeys("YOUR PASSWORD");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("signIn")).click();
driver.findElement(By.xpath("//div[@class='z0']/div")).click();
driver.findElement(By.xpath("//div[@class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
driver.findElement(By.xpath("//*[@id='gb_71']")).click();
driver.close();
}
}
So in this article, we discussed about automating the Gmail Login and Sign-out functionality using WebDriver.
Hope this helps beginners like me… :)
CodeProject