Skip to content

Commit

Permalink
fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MohabMohie committed Mar 21, 2024
1 parent 677f5e5 commit d90500b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 91 deletions.
34 changes: 20 additions & 14 deletions src/test/java/mockito/DatabaseActionsTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mockito;

import com.shaft.driver.SHAFT;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.sql.ResultSet;
Expand All @@ -10,46 +11,51 @@

public class DatabaseActionsTests {
// mock creation
SHAFT.DB mockedDatabaseInstance = mock();
ResultSet selectResults = mock();
ThreadLocal<SHAFT.DB> mockedDatabaseInstance = new ThreadLocal<>();

@BeforeMethod
public void setupMocks() {
mockedDatabaseInstance.set(mock());
}
@Test
public void select() {
ThreadLocal<ResultSet> selectResults = new ThreadLocal<>();
selectResults.set(mock());
// using mock object
mockedDatabaseInstance.executeSelectQuery("SELECT");
mockedDatabaseInstance.get().executeSelectQuery("SELECT");
//stubbing
when(mockedDatabaseInstance.executeSelectQuery("SELECT")).thenReturn(selectResults);
when(mockedDatabaseInstance.get().executeSelectQuery("SELECT")).thenReturn(selectResults.get());
//verifying
SHAFT.Validations.assertThat().object(mockedDatabaseInstance.executeSelectQuery("SELECT")).isEqualTo(selectResults).perform();
SHAFT.Validations.assertThat().object(mockedDatabaseInstance.get().executeSelectQuery("SELECT")).isEqualTo(selectResults.get()).perform();
}

@Test
public void insert() {
// using mock object
mockedDatabaseInstance.executeInsertQuery("INSERT");
mockedDatabaseInstance.get().executeInsertQuery("INSERT");
//stubbing
when(mockedDatabaseInstance.executeInsertQuery("INSERT")).thenReturn(0);
when(mockedDatabaseInstance.get().executeInsertQuery("INSERT")).thenReturn(0);
//verifying
SHAFT.Validations.assertThat().object(mockedDatabaseInstance.executeInsertQuery("INSERT")).isEqualTo(0).perform();
SHAFT.Validations.assertThat().object(mockedDatabaseInstance.get().executeInsertQuery("INSERT")).isEqualTo(0).perform();
}

@Test
public void update() {
// using mock object
mockedDatabaseInstance.executeUpdateQuery("UPDATE");
mockedDatabaseInstance.get().executeUpdateQuery("UPDATE");
//stubbing
when(mockedDatabaseInstance.executeUpdateQuery("UPDATE")).thenReturn(0);
when(mockedDatabaseInstance.get().executeUpdateQuery("UPDATE")).thenReturn(0);
//verifying
SHAFT.Validations.assertThat().object(mockedDatabaseInstance.executeUpdateQuery("UPDATE")).isEqualTo(0).perform();
SHAFT.Validations.assertThat().object(mockedDatabaseInstance.get().executeUpdateQuery("UPDATE")).isEqualTo(0).perform();
}

@Test
public void delete() {
// using mock object
mockedDatabaseInstance.executeDeleteQuery("DELETE");
mockedDatabaseInstance.get().executeDeleteQuery("DELETE");
//stubbing
when(mockedDatabaseInstance.executeDeleteQuery("DELETE")).thenReturn(0);
when(mockedDatabaseInstance.get().executeDeleteQuery("DELETE")).thenReturn(0);
//verifying
SHAFT.Validations.assertThat().object(mockedDatabaseInstance.executeDeleteQuery("DELETE")).isEqualTo(0).perform();
SHAFT.Validations.assertThat().object(mockedDatabaseInstance.get().executeDeleteQuery("DELETE")).isEqualTo(0).perform();
}
}
36 changes: 14 additions & 22 deletions src/test/java/testPackage/DragAndDropTests.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package testPackage;

import com.shaft.driver.DriverFactory;
import com.shaft.driver.internal.DriverFactory.DriverFactoryHelper;
import com.shaft.gui.browser.BrowserActions;
import com.shaft.driver.SHAFT;
import com.shaft.gui.element.ElementActions;
import com.shaft.gui.element.TouchActions;
import com.shaft.tools.io.ReportManager;
import com.shaft.validation.Validations;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class DragAndDropTests {
private static final ThreadLocal<WebDriver> driver = new ThreadLocal<>();
DriverFactoryHelper helper;

private static final ThreadLocal<SHAFT.GUI.WebDriver> driver = new ThreadLocal<>();
@Test(description = "TC001 - Test Drag and Drop function.")
public void dragAndDrop() {
new BrowserActions(helper).navigateToURL("http://the-internet.herokuapp.com/drag_and_drop"); // PASSED
driver.get().browser().navigateToURL("http://the-internet.herokuapp.com/drag_and_drop"); // PASSED
By dropDestinationLocator = By.xpath("//div[@id='columns']//*[contains (text(),'B')]");
By dragTarget1Locator = By.xpath("//div[@id='columns']//*[contains (text(),'A')]");

Expand All @@ -33,22 +27,21 @@ public void dragAndDrop() {

// ElementActions.click(driver, dragTarget1Locator);
ReportManager.log("Attempting Drag and Drop");
new ElementActions(helper).dragAndDrop(dragTarget1Locator, dropDestinationLocator);
driver.get().element().dragAndDrop(dragTarget1Locator, dropDestinationLocator);

}

@Test(description = "TC002 - Test Drag and Drop by offset function.")
public void dragAndDropByOffset() {
new BrowserActions(helper).navigateToURL("https://jqueryui.com/resources/demos/draggable/default.html");
driver.get().browser().navigateToURL("https://jqueryui.com/resources/demos/draggable/default.html");
By dragTargetLocator = By.id("draggable");

new ElementActions(helper).dragAndDropByOffset(dragTargetLocator, 100, 50);
driver.get().element().dragAndDropByOffset(dragTargetLocator, 100, 50);
}

@Test
public void dragAndDropJquery() {
new BrowserActions(helper).navigateToURL("https://jqueryui.com/resources/demos/droppable/default.html");
ElementActions actions = new ElementActions(driver.get());
driver.get().browser().navigateToURL("https://jqueryui.com/resources/demos/droppable/default.html");
ElementActions actions = driver.get().element();
String initialDroppableText = actions.getText(By.id("droppable"));
actions.dragAndDrop(By.id("draggable"), By.id("droppable"));
String finalDroppableText = actions.getText(By.id("droppable"));
Expand All @@ -59,8 +52,8 @@ public void dragAndDropJquery() {

@Test
public void dragAndDropTouchEnabled() {
new BrowserActions(helper).navigateToURL("https://jqueryui.com/resources/demos/droppable/default.html");
ElementActions actions = new ElementActions(helper);
driver.get().browser().navigateToURL("https://jqueryui.com/resources/demos/droppable/default.html");
ElementActions actions = driver.get().element();
String initialDroppableText = actions.getText(By.id("droppable"));
actions.touch().swipeToElement(By.id("draggable"), By.id("droppable"));
String finalDroppableText = actions.getText(By.id("droppable"));
Expand All @@ -71,19 +64,18 @@ public void dragAndDropTouchEnabled() {

@Test
public void dragAndDropByOffsetTouchEnabled() {
new BrowserActions(helper).navigateToURL("https://jqueryui.com/resources/demos/draggable/default.html");
driver.get().browser().navigateToURL("https://jqueryui.com/resources/demos/draggable/default.html");
By dragTargetLocator = By.id("draggable");
new TouchActions(helper).swipeByOffset(dragTargetLocator, 100, 50);
driver.get().touch().swipeByOffset(dragTargetLocator, 100, 50);
}

@BeforeMethod
public void beforeMethod() {
helper = new DriverFactory().getHelper();
driver.set(helper.getDriver());
driver.set(new SHAFT.GUI.WebDriver());
}

@AfterMethod(alwaysRun = true)
public void afterMethod() {
new BrowserActions(helper).closeCurrentWindow();
driver.get().quit();
}
}
21 changes: 10 additions & 11 deletions src/test/java/testPackage/DynamicLoadingTest.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
package testPackage;

import com.shaft.driver.DriverFactory;
import com.shaft.driver.SHAFT;
import com.shaft.gui.browser.BrowserActions;
import com.shaft.gui.element.ElementActions;
import com.shaft.validation.Validations;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class DynamicLoadingTest {
private static final ThreadLocal<WebDriver> driver = new ThreadLocal<>();
private static final ThreadLocal<SHAFT.GUI.WebDriver> driver = new ThreadLocal<>();

@Test
public void dynamicLoading_elementIsHidden() {
new BrowserActions(driver.get()).navigateToURL("https://the-internet.herokuapp.com/dynamic_loading/1");
new ElementActions(driver.get()).click(By.xpath("//button[text()='Start']"));
Validations.assertThat().element(driver.get(), By.id("finish")).text().contains("Hello World!").perform();
new BrowserActions(driver.get().getDriver()).navigateToURL("https://the-internet.herokuapp.com/dynamic_loading/1");
new ElementActions(driver.get().getDriver()).click(By.xpath("//button[text()='Start']"));
Validations.assertThat().element(driver.get().getDriver(), By.id("finish")).text().contains("Hello World!").perform();
}

@Test
public void dynamicLoading_elementIsRendered() {
new BrowserActions(driver.get()).navigateToURL("https://the-internet.herokuapp.com/dynamic_loading/2");
new ElementActions(driver.get()).click(By.xpath("//button[text()='Start']"));
Validations.assertThat().element(driver.get(), By.id("finish")).text().contains("Hello World!").perform();
new BrowserActions(driver.get().getDriver()).navigateToURL("https://the-internet.herokuapp.com/dynamic_loading/2");
new ElementActions(driver.get().getDriver()).click(By.xpath("//button[text()='Start']"));
Validations.assertThat().element(driver.get().getDriver(), By.id("finish")).text().contains("Hello World!").perform();
}

@BeforeMethod
public void beforeMethod() {
driver.set(new DriverFactory().getDriver());
driver.set(new SHAFT.GUI.WebDriver());
}

@AfterMethod(alwaysRun = true)
public void afterMethod() {
new BrowserActions(driver.get()).closeCurrentWindow();
driver.get().quit();
}
}
88 changes: 44 additions & 44 deletions src/test/java/testPackage/unitTests/APITests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,114 +6,114 @@
import java.util.List;

public class APITests {
SHAFT.API driver;
ThreadLocal<SHAFT.API> driver = new ThreadLocal<>();

@Test
public void test_jsonValueAdvancedEvaluation_filterExpression() {
driver = new SHAFT.API("https://jsonplaceholder.typicode.com");
driver.get("/users").perform();
driver.assertThatResponse().extractedJsonValue("$[?(@.name=='Chelsey Dietrich')].id").isEqualTo("5").perform();
driver.set(new SHAFT.API("https://jsonplaceholder.typicode.com"));
driver.get().get("/users").perform();
driver.get().assertThatResponse().extractedJsonValue("$[?(@.name=='Chelsey Dietrich')].id").isEqualTo("5").perform();
}

@Test
public void test_jsonValueAdvancedEvaluation_arrayIndex() {
driver = new SHAFT.API("https://jsonplaceholder.typicode.com");
driver.get("/users").perform();
driver.assertThatResponse().extractedJsonValue("$[3].address.city").isEqualTo("South Elvis").perform();
driver.set(new SHAFT.API("https://jsonplaceholder.typicode.com"));
driver.get().get("/users").perform();
driver.get().assertThatResponse().extractedJsonValue("$[3].address.city").isEqualTo("South Elvis").perform();
}

@Test
public void test_jsonValueAdvancedEvaluation_numbers() {
driver = new SHAFT.API("https://jsonplaceholder.typicode.com");
driver.get("/users").perform();
driver.assertThatResponse().extractedJsonValue("$[3].id").isEqualTo("4").perform();
driver.set(new SHAFT.API("https://jsonplaceholder.typicode.com"));
driver.get().get("/users").perform();
driver.get().assertThatResponse().extractedJsonValue("$[3].id").isEqualTo("4").perform();
}

@Test
public void test_jsonValueAdvancedEvaluation_boolean() {
driver = new SHAFT.API("https://jsonplaceholder.typicode.com");
driver.get("/todos").perform();
driver.assertThatResponse().extractedJsonValue("$[1].completed").isEqualTo("false").perform();
driver.set(new SHAFT.API("https://jsonplaceholder.typicode.com"));
driver.get().get("/todos").perform();
driver.get().assertThatResponse().extractedJsonValue("$[1].completed").isEqualTo("false").perform();
}

@Test
public void test_jsonValue_htmlResponse() {
driver = new SHAFT.API("https://automationexercise.com/api");
driver.get("/productsList").perform();
driver.assertThatResponse().extractedJsonValue("responseCode").isEqualTo("200").perform();
driver.set(new SHAFT.API("https://automationexercise.com/api"));
driver.get().get("/productsList").perform();
driver.get().assertThatResponse().extractedJsonValue("responseCode").isEqualTo("200").perform();
}

@Test
public void test_jsonValueAdvancedEvaluation_htmlResponse() {
driver = new SHAFT.API("https://automationexercise.com/api");
driver.get("/productsList").perform();
driver.assertThatResponse().extractedJsonValue("$.products[?(@.id=='2')].name").isEqualTo("Men Tshirt").perform();
driver.set(new SHAFT.API("https://automationexercise.com/api"));
driver.get().get("/productsList").perform();
driver.get().assertThatResponse().extractedJsonValue("$.products[?(@.id=='2')].name").isEqualTo("Men Tshirt").perform();
}

///////////////////////// Getters /////////////////////////
@Test
public void test_getResponseBody() {
driver = new SHAFT.API("http://api.zippopotam.us/");
driver.get("us/90210").perform();
String body = driver.getResponseBody();
driver.set(new SHAFT.API("http://api.zippopotam.us/"));
driver.get().get("us/90210").perform();
String body = driver.get().getResponseBody();
SHAFT.Validations.assertThat().object(body).contains("Beverly Hills").perform();
}

@Test
public void test_getResponseStatusCode() {
driver = new SHAFT.API("http://api.zippopotam.us/");
driver.get("us/90210").perform();
int statusCode = driver.getResponseStatusCode();
driver.set(new SHAFT.API("http://api.zippopotam.us/"));
driver.get().get("us/90210").perform();
int statusCode = driver.get().getResponseStatusCode();
SHAFT.Validations.assertThat().number(statusCode).isEqualTo(200).perform();
}

@Test
public void test_getResponseTime() {
driver = new SHAFT.API("http://api.zippopotam.us/");
driver.get("us/90210").perform();
long responseTime = driver.getResponseTime();
driver.set(new SHAFT.API("http://api.zippopotam.us/"));
driver.get().get("us/90210").perform();
long responseTime = driver.get().getResponseTime();
SHAFT.Validations.verifyThat().number(responseTime).isGreaterThanOrEquals(1.1).perform();
SHAFT.Validations.verifyThat().number(responseTime).isLessThanOrEquals(100000).perform();
}

@Test
public void test_getResponseJSONValue() {
driver = new SHAFT.API("https://jsonplaceholder.typicode.com");
driver.get("/users").perform();
String value = driver.getResponseJSONValue("$[?(@.name=='Ervin Howell')].address.street");
driver.set(new SHAFT.API("https://jsonplaceholder.typicode.com"));
driver.get().get("/users").perform();
String value = driver.get().getResponseJSONValue("$[?(@.name=='Ervin Howell')].address.street");
SHAFT.Validations.assertThat().object(value).isEqualTo("Victor Plains").perform();
}

@Test
public void test_getResponseJSONValueAsList() {
driver = new SHAFT.API("https://jsonplaceholder.typicode.com");
driver.get("/todos").perform();
List<Object> completedList = driver.getResponseJSONValueAsList("$[?(@.completed==true)].completed");
driver.set(new SHAFT.API("https://jsonplaceholder.typicode.com"));
driver.get().get("/todos").perform();
List<Object> completedList = driver.get().getResponseJSONValueAsList("$[?(@.completed==true)].completed");
for (Object completed : completedList) {
SHAFT.Validations.verifyThat().object(completed.toString()).isEqualTo("true").perform();
}
}

@Test
public void test_getResponseJSONValueAsList_validation() {
driver = new SHAFT.API("https://jsonplaceholder.typicode.com");
driver.get("/todos").perform();
driver.verifyThatResponse().extractedJsonValueAsList("$[?(@.completed==true)].completed").isEqualTo("true").perform();
driver.set(new SHAFT.API("https://jsonplaceholder.typicode.com"));
driver.get().get("/todos").perform();
driver.get().verifyThatResponse().extractedJsonValueAsList("$[?(@.completed==true)].completed").isEqualTo("true").perform();
}

@Test
public void test_validationWizard_getResponseBody() {
driver = new SHAFT.API("http://api.zippopotam.us/");
driver.get("us/90210").perform();
driver.assertThatResponse().body().contains("Beverly Hills").perform();
driver.set(new SHAFT.API("http://api.zippopotam.us/"));
driver.get().get("us/90210").perform();
driver.get().assertThatResponse().body().contains("Beverly Hills").perform();
}

@Test
public void test_validationWizard_getResponseTime() {
driver = new SHAFT.API("https://jsonplaceholder.typicode.com");
driver.get("/users").perform();
driver.verifyThatResponse().time().isGreaterThanOrEquals(1).perform();
driver.assertThatResponse().time().isLessThanOrEquals(100000.1).perform();
driver.set(new SHAFT.API("https://jsonplaceholder.typicode.com"));
driver.get().get("/users").perform();
driver.get().verifyThatResponse().time().isGreaterThanOrEquals(1).perform();
driver.get().assertThatResponse().time().isLessThanOrEquals(100000.1).perform();
}

}

0 comments on commit d90500b

Please sign in to comment.