Below is the code to automate Amazon.com from search to payment page and it works perfectly fine. I am new to selenium. SO what factors I should keep in mind while writing a script? and What more improvement I can make to this code? please advice. Is this the correct way to write script using Selenium web driver? how I can improve this code ?
WebDriver driver;
@BeforeTest
public void connection()
{
System.setProperty("webdriver.chrome.driver", "\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.amazon.com/");
driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@Test (priority =1)
public void select_department() throws InterruptedException
{
driver.findElement(By.xpath("//div[@class='nav-search-scope nav-sprite']")).click();
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//select[@id='searchDropdownBox']")));
Select department = new Select(driver.findElement(By.xpath("//select[@id='searchDropdownBox']")));
department.selectByVisibleText("Appliances");
driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).sendKeys("microwave");
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[@class='nav-search-submit nav-sprite']//input[@class='nav-input']")).click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Test (priority =2)
public void filters() throws InterruptedException
{
// Click on Brand filter
String brand = "Panasonic";
WebElement ul= driver.findElement(By.xpath("//div[@id='brandsRefinements']//ul[@class='a-unordered-list a-nostyle a-vertical a-spacing-medium']"));
List<WebElement> li = ul.findElements(By.xpath("//span[@class='a-size-base a-color-base']"));
for(int i=0;i<li.size();i++)
{
String span = li.get(i).getText();
if(span.equals(brand))
{
li.get(i).click();
break;
}
}
// Price Filter
driver.findElement(By.xpath("//input[@id='low-price']")).sendKeys("50");
driver.findElement(By.xpath("//input[@id='high-price']")).sendKeys("100");
driver.findElement(By.xpath("//span[@id='a-autoid-1']//input[@class='a-button-input']")).click();
}
@Test(priority= 3)
public void listepage() throws InterruptedException
{
String URL = driver.getCurrentUrl();
//Scroll down the page
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,1000)", "");
// Select a random Item from list page
driver.findElement(By.xpath("//span[contains(text(),'Panasonic Microwave Oven NN-SD945S Stainless Steel')]")).click();
driver.findElement(By.xpath("//input[@id='add-to-cart-button']")).click();
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='attach-accessory-pane']/div/div[1]")));
driver.findElement(By.xpath("//*[@id='attach-sidesheet-view-cart-button']/span/input")).click();
driver.navigate().to(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
jse.executeScript("window.scrollBy(0,1000)", "");
driver.findElement(By.xpath("//span[contains(text(),'Panasonic NN-SB646S Countertop Microwave Oven')]")).click();
driver.findElement(By.xpath("//input[@id='add-to-cart-button']")).click();
// driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
Thread.sleep(10000);
if(driver.findElement(By.xpath("//*[@id='a-popover-7']")).isEnabled())
{
driver.findElement(By.xpath("//*[@id='siNoCoverage-announce']")).click();
}
else
{
System.out.println("==Protection plan div is not enabled");
}
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='huc-v2-order-row-container']")));
driver.findElement(By.xpath("//a[@id='hlb-ptc-btn-native']")).click();
}
@Test(priority= 4)
public void signin ()
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@id='ap_email']")).sendKeys("");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@id='continue']")).submit();
driver.findElement(By.xpath("//*[@id='ap_password']")).sendKeys("");
driver.findElement(By.xpath("//*[@id='signInSubmit']")).click();
}
@Test(priority= 5)
public void delivery_payment_info() throws IOException
{
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[contains(text(),'Deliver to this address')]")).click();
driver.findElement(By.xpath("//input[@id='order_0_ShippingSpeed_sss-us']")).click();
driver.findElement(By.xpath("//div[contains(@class,'a-row a-spacing-medium')]//input[@class='a-button-text']")).click();
driver.findElement(By.xpath("//*[@name='ppw-accountHolderName']")).sendKeys("test test");
driver.findElement(By.xpath("//*[@name='addCreditCardNumber']")).sendKeys("2");
driver.findElement(By.xpath("//input[contains(@name,'ppw-widgetEvent:AddCreditCardEvent')]")).click();
String error_msg =driver.findElement(By.xpath("/html/body/div[5]/div/div[2]/div[3]/div/div[2]/div[2]/div[1]/div/div[1]/div/div[2]/div/form/div[1]/div/div/div")).getText();
System.out.println(error_msg);
File screenshot =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("C:\\test123.jpeg"));
}