-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to bulk apply a driver object to all Query objects in a c…
…lass
- Loading branch information
Showing
6 changed files
with
160 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/lazerycode/selenium/util/AssignDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.lazerycode.selenium.util; | ||
|
||
import org.openqa.selenium.remote.RemoteWebDriver; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class AssignDriver { | ||
public static void initQueryObjects(Object object, RemoteWebDriver driver) { | ||
Class<?> clazz = object.getClass(); | ||
// Object obj = null; | ||
try { | ||
// obj = clazz.newInstance(); | ||
Field[] fields = clazz.getDeclaredFields(); | ||
for (Field field : fields) { | ||
if (field.getType() == Query.class) { | ||
field.setAccessible(true); | ||
Query queryObject = (Query) field.get(object); | ||
if (null != queryObject) { | ||
queryObject.usingDriver(driver); | ||
} | ||
} | ||
} | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/java/com/lazerycode/selenium/util/AssignAppiumDriverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.lazerycode.selenium.util; | ||
|
||
import io.appium.java_client.AppiumDriver; | ||
import io.appium.java_client.remote.MobilePlatform; | ||
import org.junit.Test; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.Capabilities; | ||
import org.openqa.selenium.Platform; | ||
|
||
import static com.lazerycode.selenium.util.AssignDriver.initQueryObjects; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME; | ||
|
||
public class AssignAppiumDriverTest { | ||
private static final AppiumDriver MOCKED_APPIUM_DRIVER = mock(AppiumDriver.class); | ||
|
||
private Query valid = new Query().defaultLocator(By.id("foo")); | ||
private Query empty; | ||
|
||
public AssignAppiumDriverTest() { | ||
Capabilities mockedCapabilities = mock(Capabilities.class); | ||
when(mockedCapabilities.getBrowserName()).thenReturn(""); | ||
when(mockedCapabilities.getCapability(PLATFORM_NAME)).thenReturn(Platform.fromString(MobilePlatform.ANDROID)); | ||
when(mockedCapabilities.getCapability("automationName")).thenReturn("Appium"); | ||
when(MOCKED_APPIUM_DRIVER.getCapabilities()).thenReturn(mockedCapabilities); | ||
|
||
initQueryObjects(this, MOCKED_APPIUM_DRIVER); | ||
} | ||
|
||
@Test | ||
public void something() { | ||
assertThat(empty).isNull(); | ||
assertThat(valid.checkDriverIsSet()).isTrue(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/test/java/com/lazerycode/selenium/util/AssignRemoteWebDriverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.lazerycode.selenium.util; | ||
|
||
import org.junit.Test; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.Capabilities; | ||
import org.openqa.selenium.Platform; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.remote.BrowserType; | ||
import org.openqa.selenium.remote.RemoteWebDriver; | ||
|
||
import static com.lazerycode.selenium.util.AssignDriver.initQueryObjects; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME; | ||
|
||
public class AssignRemoteWebDriverTest { | ||
private static final RemoteWebDriver MOCKED_CHROME_DRIVER = mock(ChromeDriver.class); | ||
|
||
private Query valid = new Query().defaultLocator(By.id("foo")); | ||
private Query empty; | ||
|
||
public AssignRemoteWebDriverTest() { | ||
Capabilities mockedCapabilities = mock(Capabilities.class); | ||
when(mockedCapabilities.getBrowserName()).thenReturn(BrowserType.GOOGLECHROME); | ||
when(mockedCapabilities.getCapability(PLATFORM_NAME)).thenReturn(Platform.YOSEMITE); | ||
when(MOCKED_CHROME_DRIVER.getCapabilities()).thenReturn(mockedCapabilities); | ||
|
||
initQueryObjects(this, MOCKED_CHROME_DRIVER); | ||
} | ||
|
||
@Test | ||
public void something() { | ||
assertThat(empty).isNull(); | ||
assertThat(valid.checkDriverIsSet()).isTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters