In the
previous article we discussed the installation of Java and configuration of Eclipse with a Selenium WebDriver. Now here we will be creating a small JavaScript. All beginners will first want to open a browser and 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 following 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 preceding scenario. In the previous article I discussed the creation of a Java Project, Package and Class. And how to import the JAR files into the project. Here we have also followed the same thing. For example, we will create a project called "Gmail" , a package as "login" and a class as "Login1". Now we will write the code as in the following.
- 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();
- }
- }
After writing this script you will see some Red lines under some element like this.
By hovering over the Bulb symbol present in the left side, you can determine which type of error it is.
You just need to hover the mouse pointer on that specific element or click on that specific element and press [Ctrl+Space] on the keyboard. Many suggestions will be listed there and you need to select the appropriate package for that element. To determine which class belongs to which package, click on this link. See the following for the images in details.
Now run the script by right-clicking on the class then select Run As -> Java Application.
OrClick on the Run Icon present in the Top Navigational Bar.
Now you will see the browser will automatically open and will perform the desired task as said above.
Conclusion:
So in this article we discussed automating the Gmail Login and Sign-out functionality using WebDriver.
I hope this helps beginners like me.