0% found this document useful (0 votes)
38 views4 pages

Phase 1 Project - Amazon

The document describes a project to search for products on Amazon, capture screenshots of the results, and store product details in a database. The project includes classes to initialize the webdriver, search for a product, get the results and store them in a list, capture screenshots, and insert products into a SQL database.

Uploaded by

gobardhan baral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Phase 1 Project - Amazon

The document describes a project to search for products on Amazon, capture screenshots of the results, and store product details in a database. The project includes classes to initialize the webdriver, search for a product, get the results and store them in a list, capture screenshots, and insert products into a SQL database.

Uploaded by

gobardhan baral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Amazon SimplieLearn Project --------- Phase1

package AmazonSimplieLearnProject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class ProductSearchPage {

WebDriver driver;

By searchField = By.id("twotabsearchtextbox");
By searchBtn = By.id("nav-search-submit-button");

By linkTxt = By.xpath(".//a[contains(@href,'/sspa/click')]");

void setDriver(WebDriver driver) {


this.driver = driver;
}

void searchAndCaptureProduct(String productName) {


openAmazonSite();

waitSeconds(10);

searchFieldFill(productName);
searchBtnClick();

waitSeconds(10);

/*captureScreenshot("Screenshot1.png");*/

scrollPage();
waitSeconds(5);
scrollPage();
waitSeconds(5);
scrollPage();
waitSeconds(5);
scrollPage();
waitSeconds(5);
/*captureScreenshot("Screenshot2.png");*/
}

List<Product> getProductList() {
List<WebElement> links = driver.findElements(linkTxt);

System.out.println(links.size());

String[] linktext = new String[links.size()];

int i = 0;
for (WebElement e : links) {
System.out.println(e);
linktext[i] = (e).getText();
i++;
}

System.out.println(linktext.length);

List<Product> products = new ArrayList<>();

for (String t : linktext) {

Product p = new Product();


p.productName = t;
p.productPrice = 0;
products.add(p);
//waitSeconds(5);
//System.out.println(t);
//driver.findElement(By.linkText(t)).click();
/*
* String at = driver.getTitle(); if (at.equals("Sony Bravia")) {
* System.out.println(t + " is sony"); } else { System.out.println(t +
* " is not sony "); }
*/
//waitSeconds(5);
//driver.navigate().back();
}
return products;
}

void openAmazonSite() {
driver.get("http://www.amazon.in/");
driver.manage().window().maximize();
}

void waitSeconds(int seconds) {


driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
}

void searchFieldFill(String value) {


WebElement searchbar = driver.findElement(searchField);
searchbar.sendKeys(value);
}

void searchBtnClick() {
WebElement search = driver.findElement(searchBtn);
search.click();
}

void scrollPage() {
JavascriptExecutor j = (JavascriptExecutor) driver;
j.executeScript("window.scrollBy(0,500)");
}

void captureScreenshot(String fileName) {

try {
File ss = (File) ((TakesScreenshot)
driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(ss, new File("C:\\Users\\laksh\\Documents\\amazon\\" +
fileName));
} catch (Exception e) {
e.printStackTrace();
}
}
}

package AmazonSimplieLearnProject;

import java.sql.SQLException;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ProductSearchExample {

public static void main(String[] args) throws ClassNotFoundException, SQLException {


// TODO Auto-generated method stub

WebDriver driver = new ChromeDriver();

ProductSearchPage ps = new ProductSearchPage();

ps.setDriver(driver);

ps.searchAndCaptureProduct("Sony Bravia 55 inches tv");

List<Product> products = ps.getProductList();

DatabaseOps dop = new DatabaseOps();


dop.initConnection();
dop.insertProductsInDb(products);
dop.readProductsFromDb();
}
}

package AmazonSimplieLearnProject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class DatabaseOps {

Connection conn;

public void initConnection() throws ClassNotFoundException, SQLException {

Class.forName("com.mysql.cj.jdbc.Driver");
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/AmazonProducts", "root", "root");

public void readProductsFromDb() throws ClassNotFoundException, SQLException {

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from amazonproducts_info");

while (rs.next()) {
System.out.println(rs.getString("productname"));
System.out.println(rs.getInt("productid"));
System.out.println(rs.getInt("productcost"));

}
conn.close();
}

public void insertProductsInDb(List<Product> products) throws ClassNotFoundException,


SQLException {
Statement stmt = conn.createStatement();

Product p;
for (int i = 0; i < products.size(); i++) {
p = products.get(i);
String sql = "INSERT INTO amazonproducts_info VALUES ('" +
p.productName + "', 123, " + p.productPrice
+ ")";
stmt.executeUpdate(sql);

}
}
}

package AmazonSimplieLearnProject;

public class Product {


String productName;
int productPrice;
}

You might also like