Selenium Assignment – 4
Here is the fourth assignment in the series of real-time scenarios. In this assignment, you will practice various features of Selenium WebDriver like selecting from a dropdown, writing dynamic XPath for lists, iterating lists, invoking JavaScript, etc.
eBay search product project:
Scenario:
- Open any browser of your choice (Mozilla firefox, Chrome, Internet Explorer or Safari). Write the code in such a way that based on argument passed respective browser is selected.
- Browse to https://in.ebay.com/ website.
- Enter a product in the search box on the homepage (say Apple Watches).
- From categories dropdown, select category of your product (say Electronics).
- Click the Search button.
- Write a method to print the result of the product.
- Write a method to print Nth product say 10th Product. (This should be a generic method)
- Write a method to print all products from 1st page.
- Write a method to print all products along with scroll down.
PS: Try this scenario and for feedback or any query, mail it to us at qatechhub@gmail.com.
Happy Learning and keep reading QA Tech Hub 🙂
Update — Please find the code of the assignment below —
package qatechhub.assignments; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.Select; public class EbaySearch { ChromeDriver driver; String url = "http://www.ebay.in"; public void invokeBrowser(){ System.setProperty("webdriver.chrome.driver", "C:\\Users\\Saurabh Dhingra\\workspace\\libs\\chromeDriver36\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().deleteAllCookies(); driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS); driver.get(url); } public void searchProduct(String product, String category){ driver.findElement(By.id("gh-ac")).sendKeys(product); WebElement dropdown = driver.findElement(By.id("gh-cat")); Select selectCategory = new Select(dropdown); selectCategory.selectByVisibleText(category); driver.findElement(By.id("gh-btn")).click(); String result = driver.findElement(By.className("listingscnt")).getText(); System.out.println("Result :: "+ result); } public void getNthProduct(int itemNumber){ String productXpath = String.format("//div[@id='ResultSetItems']/ul[@id='ListViewInner']/li[%d]", itemNumber); String nthProduct = driver.findElement(By.xpath(productXpath)).getText(); System.out.println("Nth Product :: "+ nthProduct); } public void getAllProducts(){ List<WebElement> allProduct = driver.findElements(By.xpath("//div[@id='ResultSetItems']/ul[@id='ListViewInner']/li")); for(WebElement product: allProduct){ System.out.println(product.getText()); System.out.println("-----------------------------------------"); } } public void getAllProductsViaScrollDown(){ List<WebElement> allProduct = driver.findElements(By.xpath("//div[@id='ResultSetItems']/ul[@id='ListViewInner']/li")); Actions action = new Actions(driver); for(WebElement product: allProduct){ action.moveToElement(product).build().perform(); System.out.println(product.getText()); System.out.println("-----------------------------------------"); } } public void getAllProductsViaScrollDownViaJS(){ List<WebElement> allProduct = driver.findElements(By.xpath("//div[@id='ResultSetItems']/ul[@id='ListViewInner']/li")); for(WebElement product: allProduct){ int x, y; x = product.getLocation().x; y = product.getLocation().y; scrollDown(x, y); System.out.println(product.getText()); System.out.println("-----------------------------------------"); } } private void scrollDown(int x, int y){ JavascriptExecutor jsEngine; String jsCommand = String.format("window.scrollBy(%d,%d)", x,y); jsEngine = (JavascriptExecutor) driver; jsEngine.executeScript(jsCommand); } }
Here is the link to the Fifth Assignment.