Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with desktop switch window not working #247

Merged
merged 4 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>${appium.version}</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testng</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.ios.options.XCUITestOptions;
import io.appium.java_client.windows.options.WindowsOptions;
import io.github.selcukes.commons.helper.Preconditions;
import io.github.selcukes.databind.utils.StringHelper;
import lombok.experimental.UtilityClass;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebElement;

@UtilityClass
public class AppiumOptions {
public Capabilities setAppTopLevelWindow(String windowId) {
return setCapability("appTopLevelWindow", windowId);
}

public Capabilities appRoot() {
return setCapability("app", "Root");
public MutableCapabilities getWinAppOptions(WebElement element) {
String appTopLevelWindow = element.getAttribute("NativeWindowHandle");
Preconditions.checkArgument(!appTopLevelWindow.isEmpty(),
"The found window does not have NativeWindowHandle property");
String windowIdToHex = StringHelper.toHex(appTopLevelWindow);
var options = new WindowsOptions();
options.setAppTopLevelWindow(windowIdToHex);
return merge(options);
}

public MutableCapabilities getWinAppOptions(String app) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.github.selcukes.core.driver;

import io.github.selcukes.commons.exception.DriverConnectionException;
import io.github.selcukes.commons.fixture.DriverFixture;
import io.github.selcukes.commons.helper.SingletonContext;
import io.github.selcukes.core.enums.DeviceType;
Expand Down Expand Up @@ -55,7 +56,12 @@ public class DriverManager {
*/
public synchronized <D extends WebDriver> D createDriver(DeviceType deviceType, Capabilities... capabilities) {
createDevice(deviceType, capabilities);
setDriver(DEVICE_POOL.get().getDevice(deviceType, 0));
var devices = getDevicePool().getDevices(deviceType);
if (devices.isEmpty()) {
throw new DriverConnectionException("No devices available for " + deviceType);
}
int deviceIndex = devices.size() - 1;
setDriver(getDevicePool().getDevice(deviceType, deviceIndex));
return getDriver();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
package io.github.selcukes.core.page;

import io.appium.java_client.windows.WindowsDriver;
import io.github.selcukes.commons.helper.Preconditions;
import io.github.selcukes.core.driver.AppiumOptions;
import io.github.selcukes.core.driver.DriverManager;
import io.github.selcukes.core.enums.DeviceType;
import io.github.selcukes.databind.utils.StringHelper;
import lombok.CustomLog;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

Expand All @@ -39,15 +38,10 @@ public WebDriver getDriver() {
return driver;
}

public WinPage switchToWindowBy(Object locator) {
WebElement newWindowElement = find(locator);
String appTopLevelWindow = newWindowElement.getAttribute("NativeWindowHandle");
Preconditions.checkArgument(!appTopLevelWindow.isEmpty(),
"The found window does not have NativeWindowHandle property");
String windowIdToHex = StringHelper.toHex(appTopLevelWindow);
logger.debug(() -> "Window Id: " + appTopLevelWindow + "After: " + windowIdToHex);
driver = DriverManager.createDriver(DeviceType.DESKTOP, AppiumOptions.setAppTopLevelWindow(windowIdToHex));
driver.switchTo().activeElement();
public WinPage switchToWindowByTitle(String appTitle) {
WebElement newWindowElement = find(By.name(appTitle));
driver = DriverManager.createDriver(DeviceType.DESKTOP,
AppiumOptions.getWinAppOptions(newWindowElement));
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public void testCustomOptions() {
var chromeOptions = BrowserOptions.getBrowserOptions(Browser.CHROME, "");
var edgeOptions = BrowserOptions.getBrowserOptions(Browser.EDGE, "");
DriverManager.createDriver(DeviceType.BROWSER, chromeOptions, edgeOptions);
assertTrue(DriverManager.getWrappedDriver() instanceof ChromeDriver);
DriverManager.switchDriver(DeviceType.BROWSER, 1);
assertTrue(DriverManager.getWrappedDriver() instanceof EdgeDriver);
DriverManager.switchDriver(DeviceType.BROWSER, 0);
assertTrue(DriverManager.getWrappedDriver() instanceof ChromeDriver);
}

@Test(enabled = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.github.selcukes.core.tests.win;

import io.github.selcukes.commons.annotation.Lifecycle;
import io.github.selcukes.core.driver.AppiumOptions;
import io.github.selcukes.core.page.Pages;
import io.github.selcukes.core.page.WinPage;
import org.openqa.selenium.By;
Expand All @@ -28,8 +29,19 @@ public class NotepadTest {

@Test(enabled = false)
public void notepadTest() {

WinPage page = Pages.winPage();
fillNotepad(page);
}

@Test(enabled = false)
public void switchToNotepadTest() {
String title = "Untitled - Notepad";
WinPage page = Pages.winPage(AppiumOptions.getWinAppOptions("Root"));
page.switchToWindowByTitle(title);
fillNotepad(page);
}

private void fillNotepad(WinPage page) {
By edit = By.className("Edit");
page.enter(edit, "Welcome to Selcukes !!!")
.enter(edit, Keys.ENTER)
Expand All @@ -39,5 +51,4 @@ public void notepadTest() {
.enter(edit, Keys.CONTROL + "w" + Keys.CONTROL)
.click(By.name("Don't Save"));
}

}