0

I created an automated test using JUnit and selenium on ebay that: - navigates to page, logs in, searches for items, adds them to chart, navigates to cart page and counts the items from cart.

Everything works fine but I have no clue how should a create a junit test that confirms that the test case is passing. Expected number of items from cart equals to located_elements.size(), because everything depends on the cart page when the test case comes to end.

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Ebay_06_Login_BuyItNow_Add_3_ItemsToBasket_Checkout {
    public static void main(String[] args){
//NAVIGATE TO PAGE

        System.setProperty("webdriver.chrome.driver", "D:\\work\\Selenium\\Chrome_Driver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.ebay.com");
        driver.manage().window().maximize();

//LOG IN

        driver.findElement(By.xpath("//*[@id=\"gh-ug\"]/a")).click();                                                                       // click sign in button
        driver.findElement(By.xpath("(//input[@type='text'][@placeholder='Email or username'])")).sendKeys("name@yahoo.com");       //find user field and enter user
        driver.findElement(By.xpath("(//input[@type='password'][@placeholder='Password'][@class='fld'])")).sendKeys("testautomation123!");  //find password field and enter password
        driver.findElement(By.id("sgnBt")).click();                                                                                         //click "sign in" button
        driver.findElement(By.xpath("(//div[@class='m_t10'])")).click();                                                                    //click "maybe later" button

//SEARCH FOR ITEM
        driver.findElement(By.id("gh-ac")).sendKeys("BMW E30 M3 1/43 scale");                                                               //find search field
        driver.findElement(By.xpath("//input[@type='submit'][@class='btn btn-prim gh-spr']")).click();                                      //click "search" button


//ADD 1st ITEM FROM "BUY IT NOW" RESULTS LIST TO BASKET
        driver.findElement(By.xpath("//a[@class='small cbx btn btn-s btn-ter tab tgl_button last_b'][@title='Buy It Now']")).click();       //"buy it now" items already selected from previous search
        driver.findElement(By.xpath("//li[@class='sresult lvresult clearfix li'][@r='1']//div[@class='lvpic pic img left']")).click();      //select 1st item from results list
        driver.findElement(By.id("isCartBtn_btn")).click();                                                                                 //add to cart
        driver.findElement(By.id("contShoppingBtn")).click();                                                                               //continue shopping button

        System.out.println("1st item from results list successfully ADDED TO BASKET");

//SEARCH FOR 2nd ITEM
        driver.findElement(By.id("gh-ac")).clear();                                                                                         //clear the field in order to perform new search
        driver.findElement(By.id("gh-ac")).sendKeys("BMW E46 M3 1/43 scale");                                                               //find search field
        driver.findElement(By.xpath("//input[@type='submit'][@class='btn btn-prim gh-spr']")).click();                                      //click "search" button


//ADD 2nd ITEM FROM "BUY IT NOW" RESULTS LIST TO BASKET
                                                                                                                                            //"buy it now" already selected from previous search    
        driver.findElement(By.xpath("//li[@class='sresult lvresult clearfix li'][@r='1']//div[@class='lvpic pic img left']")).click();      //select 1st item from results list
        driver.findElement(By.id("isCartBtn_btn")).click();                                                                                 //add to cart

        System.out.println("2nd item from results list successfully ADDED TO BASKET");



            List<WebElement> located_elements = driver.findElements(By.xpath("//div[contains(@id, 'sellerBucket')]"));                      //check total number of items added to cart by counting them
            System.out.println("Total number of items in cart are: " +located_elements.size());

//      driver.quit();


    }
}
1

You have to assert the locatedelements.size() with expected:

Assert.assertEquals(located_elements.size(), xxx); 
//Replace xxx with the number of elements you expect
  • Accepted answer with -1 votes. Rarely noticed this. +1 for the accepted answer. I am assuming that if the answer is accepted, it has helped the person asking the question in some way. – Aalok Jul 3 '17 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.