kazurayam / selenium-webdriver-java

Examples of the O'Reilly book "Hands-On Selenium WebDriver with Java"
https://oreil.ly/1E7CX
Apache License 2.0
0 stars 0 forks source link

compileTestJava task failed; cannot find symbol import io.appium.java_client.remote.MobileCapabilityType; Appium java-client v8 -> v9 migration problem since 13 Oct 2023 #5

Closed kazurayam closed 9 months ago

kazurayam commented 10 months ago

After the issue #4 fixed, still I got the following comilation errors:

:~/github/selenium-webdriver-java (kazurayam4)
$ ./gradlew build

> Task :selenium-webdriver-junit4:compileTestJava
/Users/kazuakiurayama/github/selenium-webdriver-java/selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch10/mobile/AppiumJUnit4Test.java:33: error: cannot find symbol
import io.appium.java_client.remote.MobileCapabilityType;
                                   ^
  symbol:   class MobileCapabilityType
  location: package io.appium.java_client.remote
/Users/kazuakiurayama/github/selenium-webdriver-java/selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch10/mobile/AppiumJUnit4Test.java:45: error: cannot find symbol
        options.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
                              ^
  symbol:   variable MobileCapabilityType
  location: class AppiumJUnit4Test
/Users/kazuakiurayama/github/selenium-webdriver-java/selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch10/mobile/AppiumJUnit4Test.java:46: error: cannot find symbol
        options.setCapability(MobileCapabilityType.DEVICE_NAME,
                              ^
  symbol:   variable MobileCapabilityType
  location: class AppiumJUnit4Test
/Users/kazuakiurayama/github/selenium-webdriver-java/selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch10/mobile/AppiumJUnit4Test.java:48: error: cannot find symbol
        options.setCapability(MobileCapabilityType.AUTOMATION_NAME,
                              ^
  symbol:   variable MobileCapabilityType
  location: class AppiumJUnit4Test
4 errors

> Task :selenium-webdriver-junit4:compileTestJava FAILED
kazurayam commented 10 months ago

selenium-webdriver-junit4/build.gradle has the following code:

    appiumVersion = "9.0.0"

I changed this interrimly to

    appiumVersion = "8.6.0"

and tried to run the test:

$ ./gradlew test

> Task :selenium-webdriver-junit4:compileTestJava
Note: /Users/kazuakiurayama/github/selenium-webdriver-java/selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch10/mobile/AppiumJUnit4Test.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

> Task :selenium-webdriver-junit4:test

io.github.bonigarcia.webdriver.junit4.ch02.helloworld.HelloWorldChromeJUnit4Test STANDARD_OUT
    2023-10-20 21:03:25 [Test worker] DEBUG i.g.bonigarcia.wdm.WebDriverManager.<init>(225) - Using WebDriverManager 5.5.3
    2023-10-20 21:03:26 [Test worker] DEBUG i.g.b.wdm.cache.ResolutionCache.checkValidity(156) - Removing resolution chrome=97 from cache (expired on Thu Jan 13 00:05:00 JST 2022)
    2023-10-20 21:03:26 [Test worker] DEBUG i.g.b.wdm.versions.VersionDetector.getBrowserVersionFromTheShell(259) - Detecting chrome version using online commands.properties
    2023-10-20 21:03:26 [Test worker] DEBUG i.g.bonigarcia.wdm.versions.Shell.runAndWaitArray(65) - Running command on the shell: [/Applications/Google Chrome.app/Contents/MacOS/Google Chrome, --version]
    2023-10-20 21:03:27 [Test worker] DEBUG i.g.bonigarcia.wdm.versions.Shell.runAndWaitArray(69) - Result: Google Chrome 118.0.5993.96
    2023-10-20 21:03:28 [Test worker] INFO  i.g.bonigarcia.wdm.WebDriverManager.resolveDriverVersion(1217) - Using chromedriver 118.0.5993.70 (resolved driver for Chrome 118)
    2023-10-20 21:03:28 [Test worker] DEBUG i.g.b.wdm.cache.ResolutionCache.putValueInResolutionCacheIfEmpty(119) - Storing resolution chrome=118 in cache (valid until 22:03:28 20/10/2023 JST)
    2023-10-20 21:03:28 [Test worker] DEBUG i.g.b.wdm.cache.ResolutionCache.putValueInResolutionCacheIfEmpty(119) - Storing resolution chrome118=118.0.5993.70 in cache (valid until 21:03:28 21/10/2023 JST)
    2023-10-20 21:03:28 [Test worker] DEBUG i.g.bonigarcia.wdm.WebDriverManager.createUrlHandler(1453) - Driver to be downloaded chromedriver 118.0.5993.70
    2023-10-20 21:03:28 [Test worker] INFO  i.g.bonigarcia.wdm.online.Downloader.downloadAndExtract(132) - Downloading https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/118.0.5993.70/mac-x64/chromedriver-mac-x64.zip
    2023-10-20 21:03:36 [Test worker] INFO  i.g.bonigarcia.wdm.online.Downloader.extract(193) - Extracting driver from compressed file chromedriver-mac-x64.zip
    2023-10-20 21:03:37 [Test worker] INFO  i.g.bonigarcia.wdm.WebDriverManager.exportDriver(1270) - Exporting webdriver.chrome.driver as /Users/kazuakiurayama/.cache/selenium/chromedriver/mac64/118.0.5993.70/chromedriver

io.github.bonigarcia.webdriver.junit4.ch02.helloworld.HelloWorldChromeJUnit4Test > test FAILED
    java.lang.NoClassDefFoundError at HelloWorldChromeJUnit4Test.java:46
        Caused by: java.lang.ClassNotFoundException at HelloWorldChromeJUnit4Test.java:46
    java.lang.NullPointerException at HelloWorldChromeJUnit4Test.java:51

io.github.bonigarcia.webdriver.junit4.ch02.helloworld.HelloWorldEdgeJUnit4Test STANDARD_OUT
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.bonigarcia.wdm.WebDriverManager.<init>(225) - Using WebDriverManager 5.5.3
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.b.wdm.versions.VersionDetector.getBrowserVersionFromTheShell(259) - Detecting edge version using online commands.properties
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.bonigarcia.wdm.versions.Shell.runAndWaitArray(65) - Running command on the shell: [/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge, -version]
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.bonigarcia.wdm.versions.Shell.runAndWaitNoLog(83) - There was a problem executing command </Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge -version> on the shell: Cannot run program "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" (in directory "."): error=2, No such file or directory
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.bonigarcia.wdm.versions.Shell.runAndWaitArray(69) - Result: 
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.b.wdm.versions.VersionDetector.getBrowserVersionFromTheShell(267) - Browser version for edge not detected using online properties (using local commands.properties)
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.bonigarcia.wdm.versions.Shell.runAndWaitArray(65) - Running command on the shell: [/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge, -version]
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.bonigarcia.wdm.versions.Shell.runAndWaitNoLog(83) - There was a problem executing command </Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge -version> on the shell: Cannot run program "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" (in directory "."): error=2, No such file or directory
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.bonigarcia.wdm.versions.Shell.runAndWaitArray(69) - Result: 
    2023-10-20 21:03:37 [Test worker] DEBUG i.g.bonigarcia.wdm.WebDriverManager.resolveDriverVersion(1242) - The driver version for EDGE is unknown ... trying with latest
    2023-10-20 21:03:38 [Test worker] DEBUG i.g.b.wdm.versions.VersionDetector.getDriverVersionFromRepository(180) - Latest version of msedgedriver according to https://msedgedriver.azureedge.net/LATEST_STABLE is 118.0.2088.57
    2023-10-20 21:03:38 [Test worker] INFO  i.g.bonigarcia.wdm.WebDriverManager.logSeekRepo(1568) - Reading https://msedgedriver.azureedge.net/?restype=container&comp=list to seek msedgedriver
    2023-10-20 21:03:41 [Test worker] DEBUG i.g.bonigarcia.wdm.WebDriverManager.createUrlHandler(1453) - Driver to be downloaded edgedriver 118.0.2088.57
    2023-10-20 21:03:41 [Test worker] INFO  i.g.bonigarcia.wdm.online.Downloader.downloadAndExtract(132) - Downloading https://msedgedriver.azureedge.net/118.0.2088.57/edgedriver_mac64.zip
    2023-10-20 21:03:49 [Test worker] INFO  i.g.bonigarcia.wdm.online.Downloader.extract(193) - Extracting driver from compressed file edgedriver_mac64.zip
    2023-10-20 21:03:49 [Test worker] INFO  i.g.bonigarcia.wdm.WebDriverManager.exportDriver(1270) - Exporting webdriver.edge.driver as /Users/kazuakiurayama/.cache/selenium/msedgedriver/mac64/118.0.2088.57/msedgedriver

io.github.bonigarcia.webdriver.junit4.ch02.helloworld.HelloWorldEdgeJUnit4Test > test FAILED
    java.lang.NoClassDefFoundError at HelloWorldEdgeJUnit4Test.java:46
        Caused by: java.lang.ExceptionInInitializerError at ClassLoader.java:-2
    java.lang.NullPointerException at HelloWorldEdgeJUnit4Test.java:51

........... (a lot more of test execution logs followed)

Now the compilation errors were worked-around by changing theappiumVersion from 9.0.0 to 8.6.0.

kazurayam commented 10 months ago

If we downgradle Appium from 9.0.0 to 8.6.0, we can pass "compileTestJava" task and therefore I can look at whole sequence of tests are running.

If we stick to the Appium 9.0.0, we can NOT pass "compileTestJava" task. This is very bad.

We need to find out how to amend the test source in java which are now failing with Appium 9.0.0 so that they pass the compilation.

I guess, the MobileCapabilityType class is no longer there in the Appium 9.0.0.

kazurayam commented 10 months ago

In the 8.6.0 Appium java-client javadoc, I found that io.appium.java_client.remote.MobileCapabilityType class was marked @Deprecated

https://javadoc.io/static/io.appium/java-client/8.6.0/io/appium/java_client/remote/MobileCapabilityType.html

io.appium.java_client.remote
Interface MobileCapabilityType
All Superinterfaces:
org.openqa.selenium.remote.CapabilityType
Deprecated. 
This interface will be removed. Use Options instead.

@Deprecated
public interface MobileCapabilityType
extends org.openqa.selenium.remote.CapabilityType
The list of common capabilities.
Read:
https://appium.io/docs/en/latest/guides/caps/#appium-capabilities

How to resolve this? --- The javadoc just says "Use Options instead".

In the Migration Guide v8 to v9 told

MobileCapabilityType interface. Use driver options instead.

kazurayam commented 10 months ago

I noticed also the compiler warned that we are using Deprecated API in the Appium java-client 8.6.0:

> Task :selenium-webdriver-junit4:compileTestJava
Note: /Users/kazuakiurayama/github/selenium-webdriver-java/selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch10/mobile/AppiumJUnit4Test.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
....
kazurayam commented 10 months ago

So how should I modify the source of AppiumJUnitXTest.java ?

According to the source of v8.6.0, MobileCapabilityType

I feel I am at a loss.


I happend to find an article https://www.lambdatest.com/blog/desired-capabilities-in-selenium-testing/

{
   “platformName”: “Android”,
    “platformVersion”: “10.0”,
    “deviceName”:  “Samsung Galaxy s10”,
    “automationName”: “Appium”,
    “app”: “path for the app under test”
}

So I guess I could rewrite the AppiumJUnitXTest.java as follows

kazurayam commented 10 months ago

The Appium java-client v9.0 README shows the following sample code:

Any generic driver that does not have a dedicated class

BaseOptions options = new BaseOptions()
    .setPlatformName("myplatform")
    .setAutomationName("mydriver")
    .amend("mycapability1", "capvalue1")
    .amend("mycapability2", "capvalue2");
AppiumDriver driver = new AppiumDriver(
    // The default URL in Appium 1 is http://127.0.0.1:4723/wd/hub
    new URL("http://127.0.0.1:4723"), options
);
try {
    WebElement el = driver.findElement(AppiumBy.className, "myClass");
    el.click();
    driver.getPageSource();
} finally {
    driver.quit();
}

How to set the DEVICE_NAME?

kazurayam commented 10 months ago

One more sample code that shows how to build Options

https://github.com/appium/java-client/blob/v9.0.0/src/test/java/io/appium/java_client/drivers/options/OptionsBuildingTest.java

import io.appium.java_client.chromium.options.ChromiumOptions;
...
    @Test
    public void canBuildChromiumOptions() {
        // Given
        // When
        ChromiumOptions options = new ChromiumOptions();

        options.setNewCommandTimeout(Duration.ofSeconds(10))
                .setPlatformName(Platform.MAC.name())
                .withBrowserName("Chrome")
                .setAutodownloadEnabled(true)
                .setBuildCheckDisabled(true)
                .setChromeDriverPort(5485)
                .setExecutable("/absolute/executable/path")
                .setLogPath("/wonderful/log/path")
                .setVerbose(true);

        // Then
        assertEquals(AutomationName.CHROMIUM, options.getAutomationName().orElse(null));
        assertEquals("Chrome", options.getBrowserName());
        assertTrue(options.isAutodownloadEnabled().orElse(null));
        assertTrue(options.isBuildCheckDisabled().orElse(null));
        assertEquals(5485, options.getChromeDriverPort().orElse(null));
        assertFalse(options.getExecutableDir().isPresent());
        assertEquals("/absolute/executable/path", options.getExecutable().orElse(null));
        assertEquals("/wonderful/log/path", options.getLogPath().orElse(null));
        assertFalse(options.isUseSystemExecutable().isPresent());
        assertTrue(options.isVerbose().orElse(null));
    }

This sample does not deal with DEVICE_NAME which our AppiumJUnitXTest class wants to specify explicitly.


I found that the above sample code uses

import io.appium.java_client.chromium.options.ChromiumOptions;

On the other hand, https://github.com/kazurayam/selenium-webdriver-java/blob/master/selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch10/mobile/AppiumJUnit4Test.java uses

import org.openqa.selenium.chrome.ChromeOptions;

This difference may have a deep background story. I should be careful.

...

Does the Appium java-client v8.6.0 has the io.appium.java_client.chromium.options.ChromiumOptions ? Let me check. ... Yes, there is. https://github.com/appium/java-client/blob/v8.6.0/src/main/java/io/appium/java_client/chromium/options/ChromiumOptions.java

Is @bonigarcia aware of io.appium.java_client.chromium.options.ChromiumOptions? Why he did not use it?

Well, I know nothing about what he thought.

What should I (@kazurayam) do? --- I (@kazurayam) want to modify the AppiumJUnit4Test.java to pass compilation. To achieve it, I will refer to the sample code that uses io.appium.java_client.chromium.options.ChromiumOptions. And I will propose a Pull Request to @bonigarcia. If he likes it or not, let him judge.

kazurayam commented 10 months ago

I found an useful stuff in the article Desired Capabilities in Selenium Testing, Lambdatest


General or Standard Desired Capabilities in Appium Testing

There are few more general capabilities like noReset, fullReset,eventTimings, etc.

kazurayam commented 10 months ago

Sample code about "deviceName"

https://stackoverflow.com/questions/39032883/how-to-get-device-name-and-app-version-using-appium-driver#

kazurayam commented 10 months ago

The Appium java-client v9.0.0 was just recently released at 13 Oct 2023

https://github.com/appium/java-client/releases/tag/v9.0.0

This icludes a breaking change:

The previously deprecated MobileCapabilityType interface has been removed. Use driver options instead

The upgrade of Appium java-client v8.6.0 to v9.0.0 has broken the https://github.com/bonigarcia/selenium-webdriver-java, tag 1.3.0

kazurayam commented 10 months ago

I have modified

at https://github.com/kazurayam/selenium-webdriver-java/commit/a2220880e88f5d56d51e9524c20c111c0f49242e

Now the compileTestJava task passes completely:

:~/github/selenium-webdriver-java (kazurayam5work *)
$ gradle clean compileTestJava
...
BUILD SUCCESSFUL in 12s
16 actionable tasks: 16 executed
kazurayam commented 10 months ago

I have to confirm the modified Appium***Test classes run appropriately. I will do that tomorro. It's late at night ...