A Small JavaScript Using Selenium Webdriver

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.
  1. Open a Firefox browser. Navigate to the URL.
  2. Maximize the window.
  3. Enter the User Name and Password.
  4. Sign-in to the Gmail Account.
  5. Click on the Compose Button.
  6.  Sign-out from the Gmail account.
  7. 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.
  1. package login;  
  2. import java.util.concurrent.TimeUnit;  
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.firefox.FirefoxDriver;  
  6. public class Login1 {  
  7. public static void main(String[] args) {  
  8. // Create a new instance of the Firefox driver  
  9. WebDriver driver = new FirefoxDriver();  
  10. //  Wait For Page To Load  
  11. // Put a Implicit wait, this means that any search for elements on the page  
  12. could take the time the implicit wait is set for before throwing exception   
  13. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
  14. // Navigate to URL  
  15. driver.get("https://mail.google.com/");  
  16. // Maximize the window.  
  17. driver.manage().window().maximize();  
  18. // Enter UserName  
  19. driver.findElement(By.id("Email")).sendKeys(" YOUR USER NAME");  
  20. // Enter Password  
  21. driver.findElement(By.id("Passwd")).sendKeys("YOUR PASSWORD");  
  22. // Wait For Page To Load  
  23. driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);  
  24. // Click on 'Sign In' button  
  25. driver.findElement(By.id("signIn")).click();  
  26. //Click on Compose Mail.  
  27. driver.findElement(By.xpath("//div[@class='z0']/div")).click();  
  28. // Click on the image icon present in the top right navigational Bar  
  29. driver.findElement(By.xpath("//div[@class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();  
  30. //Click on 'Logout' Button  
  31. driver.findElement(By.xpath("//*[@id='gb_71']")).click();  
  32. //Close the browser.  
  33. driver.close();  
  34. }  

 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.

Or

Click 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.
 


Similar Articles