Untitled

SanityTest
mail@pastecode.io avatar
unknown
java
a month ago
4.9 kB
2
Indexable
Never
package testpackage.Tests;

import net.thucydides.core.annotations.Title;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
import steps.AlertSteps;
import steps.LoginSteps;
import steps.NavigationSteps;
import steps.ProductSteps;
import testpackage.base.BaseTest;
import testpackage.entities.ProductEntity;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SanityTest extends BaseTest {

	private LoginSteps loginPage;
	private AlertSteps alertPage;
	private NavigationSteps navigation;
	private ProductSteps productSteps;
	private static JSONArray users;
	private static JSONArray products;
	private Map<String, Object> varsUserMap; // for what amit ?
	private Map<String, Object> varsProductMap;
	private static final int SLEEP_TIME_MS = 1000;

	@Before
	public void setUp() throws IOException, ParseException {
		this.loginPage = new LoginSteps(this.driver);
		this.alertPage = new AlertSteps(this.driver);
		this.navigation = new NavigationSteps(this.driver);
		this.productSteps = new ProductSteps(this.driver);
		varsUserMap = new HashMap<>();
		JSONParser jsonParser = new JSONParser();

		varsProductMap = new HashMap<>();
		JSONParser jsonParserProduct = new JSONParser();
		
		try (FileReader reader = new FileReader("src/test/java/testpackage/Tests/data.json")) {
			users = (JSONArray) jsonParser.parse(reader);
			for (Object user : users) {
				JSONObject obj = (JSONObject) user;
				System.err.println(obj.get("username"));
			}
		}
		
		try (FileReader reader = new FileReader("src/test/java/testpackage/Tests/product.json")) {
			products = (JSONArray) jsonParserProduct.parse(reader);
			for (Object product : products) {
				JSONObject obj = (JSONObject) product;
				System.err.println(obj.get("name"));
			}
		}
	}

	public void sleep() throws InterruptedException {
		Thread.sleep(SLEEP_TIME_MS);
	}

	private void addToCart(String name, int price) throws InterruptedException {
		productSteps.clickOnIphoneItem();
		sleep();
		productSteps.clickOnAddItem();
		sleep();
		alertPage.clickOk();
		sleep();
		navigation.clickOnCartLink();
		sleep();
		productSteps.verifyProductAdded(name);
		sleep();
		productSteps.verifyTotalPrice(Integer.toString(price));
		sleep();
	}



	@Test
	@Title("Login steps test")
	public void loginTest() throws InterruptedException {
		for (Object user : users) {
			JSONObject obj = (JSONObject) user;

			initializeDriver();
			navigation.clickOnLoginLink();
			sleep();
			loginPage.fillUsernameField((String) obj.get("username"));
			sleep();
			loginPage.fillPasswordField((String) obj.get("password"));
			sleep();

			String title = (String) obj.get("title");
			switch (title) {
				case "valid":
					navigation.clickOnLoginButton();
					sleep();
					loginPage.checkTextOfWelcome((String) obj.get("username"));

					break;
				case "non-valid":
					navigation.clickOnLoginButton();
					sleep();
					alertPage.verifyAlertMessage((String) obj.get("target"));
					sleep();
					this.alertPage.clickOk();

					break;
				case "click-X":
					loginPage.clickOnX();
					break;
			}
		}
	}
    @Test
	@Title("add items to cart")
	public void addingItemsTest() throws InterruptedException {
		for (Object product : products) {
			JSONObject obj = (JSONObject) product;
			initializeDriver();
			String itemName = (String) obj.get("name");	
			int price = Integer.parseInt((String) obj.get("price"));
			logger.info("all flow of sanity check - started");
			addToCart(itemName,price);
			this.navigation.clickOnHomeLink();
			sleep();
			addToCart(itemName,2*price);//720
			logger.info("all flow of sanity check - finished");
	}
	}


	/// TO DO : on amit ! test 3 and 4.
	@Test
	@Title("delete items from cart")
	public void test3(ProductEntity product) throws InterruptedException {
		this.productSteps.clickOnDeleteItem();
		sleep();
		this.productSteps.verifyTotalPrice(Integer.toString(product.getPriceString()));
		sleep();
		this.productSteps.clickOnDeleteItem();
		sleep();
		this.productSteps.verifyTotalPriceIsEmpty();
		sleep();
		this.navigation.logOut();
		logger.error("all flow of sanity check - finished");
	}
	public static void main(String[] args) {
		JUnitCore junit = new JUnitCore();
		junit.addListener(new TextListener(System.out));
		org.junit.runner.Result result = junit.run(SanityTest.class);

		if (result.getFailureCount() > 0) {
			System.out.println("Test failed.");
			System.exit(1);
		} else {
			System.out.println("Test finished successfully.");
			System.exit(0);
		}


	}
}
Leave a Comment