December 31, 2015

Automate Amazon: Writing a Sign In Test

This post is fourth of a series of nine. Need to go back to the beginning?

Drafting a Login Test for Amazon.com won't be as easy as drafting one for Dave Haeffner's mock site, The-Internet. There needs to be a lot more infrastructure put in place besides the CommonUtils library we worked on in the last blog post. Also, Amazon.com use the word "Login". Instead, they use the phrase Sign In.


Sketch the SignIn Steps

Here's how you log into sign into Amazon.com's site:
  1. Go to Amazon.com's home page, http://www.amazon.com/
  2. Hover your cursor over the "Your Account" block, and click the sign in button.
  3. Once you are on the sign in page, enter the username, password into the appropriate text boxes. 
  4. Select the Sign In button. 
I noticed after following this process a few times, that there sometimes was a bit of a lag, so we might want to wait until the username and password actually appear before entering a username and password. 

Review: How to find selectors of Web Elements

Let's say we want to know about what selectors to use for the username, password, and Sign In button on the Sign In page.

You can find the ids and cssSelectors of an object by:
  • Opening the Firefox browser with the Firebug and Firepath plugin. 
  • Going to http://www.amazon.com/ and pressing the "Sign In" button.
  • Right clicking and selecting on the textboxes or the button and selecting "Inspect in Firepath".
  • With the "Firepath" tab, make sure "CSS" is selected. 
We can see that the following web elements have these values:
  • Email textbox: cssSelector #ap_email
  • Password textbox: cssSelector #ap_password
  • Sign In Button: cssSelector #signInSubmit
... Yes, these values are technically IDs and not CSS Selectors. Adding the '#' makes them CSS Selectors. At my workplace, I find that IDs of web elements are constantly changing, since the ID is usually a numbered index. CSS Values seem to change less, so I got in the habit of leaning towards CSS Selectors over IDs. 


Sketch out the Infrastructure Needed

Judging from what we have listed above, we will need the following classes written:
  • A Test Class, PurchaseOrderTest, that stores the tests.
  • A class to store the username and password of test users, and a class to retrieve the data.
  • Methods in CommonUtilites that handle sending keys to textboxes, clicking on buttons, and hovering-and-clicking on sections of the home page.
  • Two classes called Page Objects to store how the HomePage and the LoginPage interacts with the DOM.
  • We can bundle together commonly used methods in an Actions class, and call it OrderActions.

What our Directory Structure Will Look Like


After we store the User Properties, create the Actions classes, and create the test class, our directory structure could look like:

src/test/java

  • actions
    • OrderActions
  • base
    • LoadProperties
  • pages
    • HomePage
    • SignInPage
  • properties
    • user.properties
  • testcases
    • PurchaseOrderTest
  • utils
    • CommonUtils
    • DriverUtils



Store the User Properties


Store the Username and Password in a text file called a Properties file, user.properties. Create a class called LoadProperties in order to handle the reading and the writing of the file. (Code formatting from http://codeformatter.blogspot.com/).

java / properties / user.properties:
1:  tester23.username=amzn.tester23@gmail.com  
2:  tester23.password=amzntester23  

java / base / LoadProperties.java
1:  package base;  
2:  import org.apache.commons.lang3.StringUtils;  
3:  import java.io.FileInputStream;  
4:  import java.io.FileNotFoundException;  
5:  import java.io.IOException;  
6:  import java.util.Properties;  
7:  import java.util.Set;  
8:  /**  
9:   * Created by tmaher on 12/22/2015.  
10:   */  
11:  public class LoadProperties {  
12:    public static Properties user = loadProperties("src/test/java/properties/user.properties");  
13:    private static Properties loadProperties(String filePath) {  
14:      Properties properties = new Properties();  
15:      try {  
16:        FileInputStream f = new FileInputStream(filePath);  
17:        properties.load(f);  
18:      } catch (FileNotFoundException e) {  
19:        e.printStackTrace();  
20:      } catch (IOException e){  
21:        e.printStackTrace();  
22:      }  
23:      return properties;  
24:    }  
25:    public static String getPropertyValue(String path, String key){  
26:      Properties p = loadProperties(path);  
27:      String result = "";  
28:      Set<String> values = p.stringPropertyNames();  
29:      for(String value : values){  
30:        if(StringUtils.equalsIgnoreCase(value, key)){  
31:          result = p.getProperty(value);  
32:          break;  
33:        }  
34:      }  
35:      return result;  
36:    }  
37:  }  
All Source Code can be found in T.J. Maher's Automate-Amazon repository. 

Next, we can work from the bottom level to the top:
  • Adding methods to CommonUtils
  • Abstracting each page into a PageObject such as HomePage and SignInPage
  • Bundling the methods in the PageObjects to create repeatable Actions we can use.
  • Compose the Actions methods into a test we can run. 

1. Add to CommonUtil 

Add to CommonUtils methods the basic building blocks our page objects will call. All Selenium WebDriver calls will be encapsulated in a method on CommonUtils.

  • Navigate to a URL

 public void navigateToURL(String URL) {  
     try {  
       _driver.navigate().to(URL);  
     } catch (Exception e) {  
       System.out.println("FAILURE: URL did not load: " + URL);  
       throw new TestException("URL did not load");  
     }  
   }  

  • SendKeys to a Textbox, given the web element by selector, and the value.

 public void sendKeys(By selector, String value) {  
     WebElement element = getElement(selector);  
     clearField(element);  
     try {  
       element.sendKeys(value);  
     } catch (Exception e) {  
       throw new TestException(String.format("Error in sending [%s] to the following element: [%s]", value, selector.toString()));  
     }  
 }  

  • Click on a button, given the web element by selector

 public void click(By selector) {  
     WebElement element = getElement(selector);  
     waitForElementToBeClickable(selector);  
     try {  
       element.click();  
     } catch (Exception e) {  
       throw new TestException(String.format("The following element is not clickable: [%s]", selector));  
     }  
   }  

  • Hovers a cursor over a web element and click it
 public void scrollToThenClick(By selector) {  
     WebElement element = _driver.findElement(selector);  
     actions = new Actions(_driver);  
     try {  
       ((JavascriptExecutor) _driver).executeScript("arguments[0].scrollIntoView(true);", element);  
       actions.moveToElement(element).perform();  
       actions.click(element).perform();  
     } catch (Exception e) {  
       throw new TestException(String.format("The following element is not clickable: [%s]", element.toString()));  
     }  
   }  


  • Waits Until an Element is Visible
 public void waitForElementToBeVisible(By selector) {  
     try {  
       wait = new WebDriverWait(_driver, timeout);  
       wait.until(ExpectedConditions.presenceOfElementLocated(selector));  
     } catch (Exception e) {  
       throw new NoSuchElementException(String.format("The following element was not visible: %s", selector));  
     }  
   }  

2. Page Objects

Now that the Selenium WebDriver functionality has been encapsulated, we can extend the page objects to use CommonUtils. We are inheriting from CommonUtilites the above methods.

These Page Objects are the only places that should interact with the web elements on the page, evaluating if the web elements are there, checking and setting their values.

On the top of each Page Object, we have the selectors for each web element. They are each declared private because they are only to be accessed in this page object. They are also declared final because they are constant.

By is a reserved word in the Selenium library. Web elements can be found By.id, By.cssSelector, By.name, etc.



HomePage:
1:  package pages;  
2:  import enums.Url;  
3:  import org.openqa.selenium.By;  
4:  import utils.CommonUtils;  
5:  /**  
6:   * Created by tmaher on 12/21/2015.  
7:   */  
8:  public class HomePage extends CommonUtils {  
9:    private final By YOUR_ACCOUNT = By.id("nav-link-yourAccount");  
10:    private final By SHOPPING_CART_ICON = By.cssSelector("#nav-cart");  
11:    private final By SHOPPING_CART_COUNT = By.cssSelector("#nav-cart > #nav-cart-count");  
12:    public HomePage(){  
13:    }  
14:    public void navigateToHomePage() {  
15:      String url = Url.BASEURL.getURL();  
16:      System.out.println("Navigating to Amazon.com: " + url);  
17:      navigateToURL(url);  
18:    }  
19:    public void navigateToSignInPage(){  
20:      System.out.println("HOME_PAGE: Selecting [YOUR_ACCOUNT] in navigation bar.");  
21:      scrollToThenClick(YOUR_ACCOUNT);  
22:      System.out.println("HOME_PAGE: Navigating to the SIGNIN_PAGE.\n");  
23:    }  
24:  }  

SignInPage:
1:  package pages;  
2:  import org.openqa.selenium.By;  
3:  import utils.CommonUtils;  
4:  /**  
5:   * Created by tmaher on 12/21/2015.  
6:   */  
7:  public class SignInPage extends CommonUtils {  
8:    private final By USERNAME = By.cssSelector("#ap_email");  
9:    private final By PASSWORD = By.cssSelector("#ap_password");  
10:    private final By SIGNIN_BUTTON = By.cssSelector("#signInSubmit");  
11:    public void enterUsername(String userName){  
12:      System.out.println("SIGNIN_PAGE: Entering username: " + userName);  
13:      waitForElementToBeVisible(USERNAME);  
14:      sendKeys(USERNAME, userName);  
15:    }  
16:    public void enterPassword(String password){  
17:      System.out.println("SIGNIN_PAGE: Entering password.");  
18:      waitForElementToBeVisible(PASSWORD);  
19:      sendKeys(PASSWORD, password);  
20:    }  
21:    public void clickSignInButton(){  
22:      System.out.println("SIGNIN_PAGE: Clicking the [SIGN_IN] button.\n");  
23:      click(SIGNIN_BUTTON);  
24:    }  
25:  }  

3. Actions Classes

When someone enters a username into the SignIn page, it's a safe bet that they are going to want to enter a password, too, along with clicking on the SignIn button. If someone wants to write a test that explicitly asserts that yes, we can enter text into the username textbox, they can do that by accessing the SignIn page object. But most of the time, those three methods will be grouped together.

Below, we group them together in a method called loginAs(String username, String password). Pass in a username and password, and it will do the rest.

OrderActions.java
1:  package actions;  
2:  import pages.*;  
3:  /**  
4:   * Created by tmaher on 12/21/2015.  
5:   */  
6:  public class OrderActions {  
7:    public void navigateToHomePage(){  
8:      HomePage homePage = new HomePage(); // Instantiate a new HomePage page object 
9:      homePage.navigateToHomePage();  
10:    }  
11:    public void loginAs(String username, String password){  
12:      HomePage homePage = new HomePage();  // Instantiate a new HomePage page object 
13:      SignInPage signIn = new SignInPage();  // Instantiate a new SignInPage page object 
14:      homePage.navigateToSignInPage();  
15:      signIn.enterUsername(username);  // Use the methods on the SignInPage
16:      signIn.enterPassword(password);  
17:      signIn.clickSignInButton();  
18:    }  
19:  }  


4. PurchaseOrderTest: test_Login

Now that we have all the components, we can start assembling the pieces in the Actions class to form a test.

PurchaseOrderTest.java
1:  package testcases;  
2:  import base.LoadProperties;  
3:  import org.openqa.selenium.WebDriver;  
4:  import actions.*;  
5:  import org.testng.annotations.AfterClass;  
6:  import org.testng.annotations.BeforeClass;  
7:  import org.testng.annotations.Test;  
8:  import utils.DriverUtils;  
9:  import static org.testng.Assert.assertEquals;  
10:  /**  
11:   * Created by tmaher on 12/14/2015.  
12:   */  
13:  public class PurchaseOrderTest {  
14:    public WebDriver driver;  
15:    @BeforeClass  
16:    public void setUp(){  
17:      driver = DriverUtils.getDriver();  
18:    }  
19:    @Test()  
20:    public void test_Login(){  
21:      OrderActions orderActions = new OrderActions();  
22:      String username = LoadProperties.user.getProperty("tester23.username");  
23:      String password = LoadProperties.user.getProperty("tester23.password");  
24:      orderActions.navigateToHomePage();  
25:      orderActions.loginAs(username, password);  
26:   
27:    }  
28:    @AfterClass  
29:    public void tearDown(){  
30:      driver.quit();  
31:    }  
32:  }  
All Source Code can be found in T.J. Maher's Automate-Amazon repository. 


5. Run the Test


Now that everything is all set, we can right click on the test method for test_Login and run the test:

 Navigating to Amazon.com: http://www.amazon.com  
 HOME_PAGE: Selecting [YOUR_ACCOUNT] in navigation bar.  
 HOME_PAGE: Navigating to the SIGNIN_PAGE.  
 SIGNIN_PAGE: Entering username: amzn.tester23@gmail.com  
 SIGNIN_PAGE: Entering password.  
 SIGNIN_PAGE: Clicking the [SIGN_IN] button.  
All Source Code can be found in T.J. Maher's Automate-Amazon repository. 

... Normally, we would then assert if we are on the correct page or not. I didn't add that into the test since what I really wanted to do was the next blog entry.

Until then, happy coding!

View the (mostly) Completed Test Code:


NEXT: Setup Objects to Handle Various Products, such as Books! >> 


-T.J. Maher
 Sr. QA Engineer, Fitbit
 Boston, MA

// Automated tester for [ 8 ] month and counting!

Please note: 'Adventures in Automation' is a personal blog about automated testing. It is not an official blog of Fitbit.com



244 comments:

«Oldest   ‹Older   201 – 244 of 244
Asin said...

Nice coding , well written Digital marketing courses in Gujarat

Akhtar Iqbal said...


This is the blog post truly what I was seeking. Many credits for discussing this effective related information with magnificent preference of words.
Muhammad Ali
Akhtar Iqbal
Muhammad Usman

Akhtar Iqbal said...

Fantastic and essential points reviewed in your message customarily.
Muhammad Usman
Akhtar Iqbal
Muhammad Ali

punithaselvaraj said...

Your blog very helpful for learners, good work.
Are you looking any financial modelling course in India? Financial modelling skills are increasingly important to upgrade your career and work in the ever-growing finance sector. We have listed the best colleges in India that provide financial modelling courses in this article. Continue reading to choose the best course for you.
Financial Modeling Courses in India

Anonymous said...

The article shared is really informative and your efforts are really commendable. Keep it up. Digital Marketing Courses in Faridabad

Vikas said...

Great work, thanks for sharing the very informative article on test automation with a descriptive manner with code instances with detailed notes and directory structure with complete projection process. Truly an incredible article. If anyone wants to build his carrier in Digital Marketing then you must go through our curriculum which is designed very professionally with cutting edge of the current requirement of the corporates and based on market trends. You will be taught in a highly professional environment with practical assignments. You can decide your specialised stream your own way by understanding each subject in depth under the guidance of highly professional and experienced trainers. For more detail Please visit at
Digital Marketing Courses in Austria

Akhtar Iqbal said...

Message is definitely well composed and all the opinions are grouped in a specialist way.
TheInformationCenter
Hibbah Sheikh
All News

nancy yadav said...

all the point written in Writing a Sign In Test like review, infrastructure and all are in sequence. these points are clearly understandable. please share more this kind of information. thanks for this. Digital marketing Courses in Bhutan

Divya said...

I appreciate your efforts, and I really want to thank you for sharing the extremely useful article on test automation that you wrote in such a thorough style, along with code examples, detailed explanations, and directory structures.
Digital marketing courses in Nashik

Anwesha said...

what an awesome writing with vivid description and insightful discussion on the topic.
Digital marketing courses in Raipur

Divya said...

Although the subject is relatively new to me, I find it to be descriptive, which helps me understand the concept better. We appreciate you teaching us on this subject.
Data Analytics Courses In Kolkata

Arpitha said...

I find this blog very informational. Thanks for sharing this blog.
Data Analytics Courses in Agra

Aarika said...

Hi, Thank you blogger for the efforts you have but in to give us such an excellent and useful blog. The content is well explained with each step well described before the step by step progress. I have bookmarked this for future references. I am sure the readers would really thank you for your efforts.
Data Analytics Courses In Kochi

Ranvi singh said...

this post shows the hard work that you have put into this article. Really appreciate your effort. It is not easy to come up with this depth of knowledge. Very informative post.
Digital marketing courses in Chennai


theory said...

great website, checkout Digital Marketing Company In Pune

Abdul Basith. said...

Interesting and very valuable post for people who wants to know about Automate Amazon: Writing a Sign In Test. The post covers the basics of setting up a sign in test for Amazon using the Automate tool. It's a quick read and gives a good overview of the process. Digital Marketing Courses in Australia

simmu thind said...

I genuinely appreciate your help in coming up with such good articles and presenting them in such a great sequence. thanks for sharing this content to us. If your are looking for professional courses after senior secondary here is the list of top 20 professional courses please check on this link - Professional Courses

Abdul Basith said...

This blog post is a great guide on how to write a sign in test for Amazon. It provides clear overview of the process and gives a clear instructions on how to get started. thanks for sharing!keep up the good work. Digital Marketing Courses in Vancouver

Anonymous said...

The technicalities of this article is really exceptional and gives learning ideas from it. Also, keep sharing such good posts. Data Analytics Courses in Delhi

abdul basith said...

This is a great article on how to automate Amazon writing sign in tests. The author provides clear instructions and screenshots to help readers follow along. This is a Great option for someone looking to boost their testing process. Thanks for sharing! Data Analytics Courses in Mumbai

Abdul basith said...

This is a great tool for automating sign in tests on Amazon. referring to this blog it making it work to quick and easy to use, and it's a great way to ensure that your sign in tests are always accurate. Thanks for sharing! Data Analytics Courses in Gurgaon

Klevy Data Analytics Courses said...

Thanks for sharing your valuable knowledge with us. Such a complex topic learn about Writing a Sign In Test in amazon.com. However you have simplified things so that anyone can better understand. For sure, this will help many learners. Keep writing.
We also provide an informational and educational blog about Data Analytics Courses In Nashik. Today Data Analytics have also gained a vast importance in this modern world. The demand for people looking for Best Data Analytics Courses In Nashik has significantly increased. People as seeing Data Analyst as a good career opportunity. However, before enrolling in Data Analytics Courses, there is a need to have some answers to these questions:
Which is the best Institute for Data Analytics Courses In Nashik?
How helpful are Data Analytics Courses In Nashik and Who may enroll in this course?
What qualifications you need to enroll in Data Analytics Courses In Nashik?
How much do Data Analytics Courses cost?
Which Skills are required for enrolling Data Analytics Courses In Nashik?
Which are Data Analytics Courses for Beginners, Intermediate and Advanced Learners?
Get more information here: Data Analytics Courses In Nashik

Abdul basith said...

This is a great post on how to automate your Amazon writing sign in test. The author provides clear instructions and screenshots to help readers follow along. This is a valuable resource for anyone who wants to learn how to automate their Amazon writing sign in test. Thanks for sharing! Data Analytics Courses In Coimbatore

Abdul basith said...

This article on automating your Amazon writing sign-in test is excellent. To make it easier for readers to follow along, the author includes screenshots and detailed directions. Anyone who wants to discover how to automate their Amazon writing sign in test will find this to be a useful resource. I appreciate you sharing!
Data Analytics Courses in Ghana

pharmaceuticalmarketing said...

thank you for sharing.check outDigital Marketing Courses In Hadapsar

Data Analytics Courses said...

Thanks for sharing this blog with us. It's amazing..  Data Analytics Courses In Bangalore 

Digital marketing course said...

Best blog I have read on automating Amazon sign in. Digital marketing courses in Varanasi

Data Analytics Course said...

This blog is written in a very descriptive manner & it's really easy to understand. Thank you so much,..  Data Analytics Courses in navi Mumbai 

Data Analytics Courses said...

Very excellent article. I really appreciate the way you presented the process in this article.  Data Analytics Courses In Bangalore 

Simran said...

Thank you for providing this useful information. Your blogs surely helps enhance my technical knowledge. Your blog always adds some great information in my learning.
Data Analytics Courses In Nagpur

DMCITRN2 said...

Truly great informative techie content. Very fruitful content is well described in a very descriptive manner with code and narratives. Thanks for sharing your great experience and hard work. If anyone wants to build his carrier in Digital Marketing then you must go through our curriculum which is designed very professionally with cutting edge of the current requirement of the corporates and based on market trends. For more detail Please visit at
Digital marketing Courses In UAE

Ajay singh said...

i adore your writing skills with the valuable content which has been shared by you. thanks for sharing. keep it up. Content Writing Courses in Delhi

RHema said...

Excellent blog on "amazon automation login test." Thanks for the step-by-step instruction on the sign-in. And the web elements are also explained well. The user property code is easy to follow, and all the in-depth descriptions are easy to implement. Thanks for the article. I have gained more understanding. Do share more.
Courses after bcom

whatsapp aero apk download said...

This is a fantastic article, and your writing is both informative and entertaining, making it a great resource for readers.

abdul basith said...

Wonderful post on automating the Amazon writing sign-in test. To make it easier for readers to follow along, the author includes screenshots and detailed directions. Anyone who wants to discover how to automate their Amazon writing sign in test will find this to be a useful resource. I appreciate you sharing!
financial modelling course in kenya

DMCIRO said...
This comment has been removed by the author.
DMCIRO said...

These kind of posts are really helpful in gaining the knowledge of unknown things which also triggers in motivating and learning the new innovative contents. Also newly ideas was amazing and well explained on web elements. Also do visit: Data Analytics Courses in New Zealand

Hema said...

This blog post on the "amazon automation login test" is excellent. I value the thorough sign-in instructions. The web components are also clearly explained. The comprehensive explanations are easy to implement, and the user property code is also straightforward to understand. The article is excellent. Keep posting more. Digital marketing courses in patna

kiran kamboj said...

this article has given me a beautiful learning about how to write a sign in test. this is such a nice article. i would like to thank you from my heart. keep sharing.Please check once for more information. Data Analytics Courses In Indore

whatsapp plus apk download said...

This is a fantastic article, and your writing is both informative and entertaining, making it a great resource for readers.

Komal_SEO_05 said...

Actually, the content is excellent and useful. I'm glad you just shared this useful knowledge with us. Please keep us updated in this manner. I appreciate you effort in sharing.
financial modeling certifications

Hema09 said...

A great blog on the "amazon automation login test." I appreciate the detailed sign-in instructions. And the web components are explained well. The user property code is simple to understand, and all the detailed explanations are simple to put into practice. I appreciate the article. I now have a better understanding. Please share more.
Financial modelling course in Singapore

Nethan Kummar said...

This article had a lot of good information, and I will try to follow your tips and instructions.

Uwatchfreemovies
Turkish series

Nethan Kummar said...
This comment has been removed by the author.
«Oldest ‹Older   201 – 244 of 244   Newer› Newest»