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

Flutter driver commands #8

Open
Jayarams99 opened this issue May 8, 2023 · 16 comments
Open

Flutter driver commands #8

Jayarams99 opened this issue May 8, 2023 · 16 comments
Labels
question Further information is requested

Comments

@Jayarams99
Copy link

Hi I am new to Flutter driver, I need help on how we can do the following.

  1. After assigning an element using flutter finder ,If I use ele.getText() or ele.isDisplayed() error is thrown as not implemented. Let me know how we can achieve the above statement
  2. How can we use findelements to find multiple elements
  3. How can we get element attributes
@harshhhh15
Copy link

  1. Text and Visibility check:
    The getText() and isDisplayed() methods are not implemented in the Element class of Flutter Driver. Instead, you can use the getTextFinder() and waitFor() methods to retrieve the text of an element and check if it is visible.
final element = find.byValueKey('my_element_key');
final textFinder = find.descendant(of: element, matching: find.text('my_text'));

await driver.waitFor(element);
final text = await driver.getText(textFinder);
print(text);

await driver.waitFor(element);
final isVisible = await driver.waitFor(element).isPresent;
print(isVisible);
  1. Finding Multiple Elements:
    To find multiple elements, you can use the findAll() method, which returns a list of Element objects that match a given Finder:
final elements = await driver.findAll(find.byValueKey('my_element_key'));

This will return a list of all elements that have a ValueKey with the value 'my_element_key'.

  1. Getting Element Attributes:
    You can use the getDiagnostics() method of an element to retrieve its attributes. This method returns a Future<String> that contains a string representation of the element and its properties.
final element = find.byValueKey('my_element_key');
final diagnostics = await driver.getDiagnostics(element);

print(diagnostics);

This will print out a string representation of the element and its attributes, including its type, size, and location on the screen. You can also use regular expressions to extract specific attributes from the diagnostic output.

final element = find.byValueKey('my_element_key');
final diagnostics = await driver.getDiagnostics(element);

final regex = RegExp(r'width=(\d+)');
final match = regex.firstMatch(diagnostics);
final width = match.group(1);

print('Element width is $width');

This code extracts the width attribute of the element from the diagnostic output and prints it to the console.

@Jayarams99
Copy link
Author

Please let me know if the same commands work in Java

@ashwithpoojary98
Copy link
Owner

Yes it should work in java as well

@Jayarams99
Copy link
Author

Thanks will try them, Can we also initialise element using @findby appium field decorators

@MohamedElbannaQC
Copy link

I couldn't access these commands while using Java - any help ?

23-05-2023 4-45-28 pm

@ashwithpoojary98
Copy link
Owner

Yes, We can execute all the commands

@MohamedElbannaQC
Copy link

@ashwithpoojary98 how can i use them in java ? is there any example ?

@ashwithpoojary98
Copy link
Owner

Yes,
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "emulator-5554");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("noReset", true);
capabilities.setCapability("app", "");
capabilities.setCapability("automationName", "Flutter");
driver = new AppiumDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));

driver.execute('flutter:checkHealth');
similarly, u can execute all the above commands

@ashwithpoojary98 ashwithpoojary98 added the question Further information is requested label Jul 8, 2023
@yyuanmeme
Copy link

  1. Text and Visibility check:
    The getText() and isDisplayed() methods are not implemented in the Element class of Flutter Driver. Instead, you can use the getTextFinder() and waitFor() methods to retrieve the text of an element and check if it is visible.
final element = find.byValueKey('my_element_key');
final textFinder = find.descendant(of: element, matching: find.text('my_text'));

await driver.waitFor(element);
final text = await driver.getText(textFinder);
print(text);

await driver.waitFor(element);
final isVisible = await driver.waitFor(element).isPresent;
print(isVisible);
  1. Finding Multiple Elements:
    To find multiple elements, you can use the findAll() method, which returns a list of Element objects that match a given Finder:
final elements = await driver.findAll(find.byValueKey('my_element_key'));

This will return a list of all elements that have a ValueKey with the value 'my_element_key'.

  1. Getting Element Attributes:
    You can use the getDiagnostics() method of an element to retrieve its attributes. This method returns a Future<String> that contains a string representation of the element and its properties.
final element = find.byValueKey('my_element_key');
final diagnostics = await driver.getDiagnostics(element);

print(diagnostics);

This will print out a string representation of the element and its attributes, including its type, size, and location on the screen. You can also use regular expressions to extract specific attributes from the diagnostic output.

final element = find.byValueKey('my_element_key');
final diagnostics = await driver.getDiagnostics(element);

final regex = RegExp(r'width=(\d+)');
final match = regex.firstMatch(diagnostics);
final width = match.group(1);

print('Element width is $width');

This code extracts the width attribute of the element from the diagnostic output and prints it to the console.

  1. Text and Visibility check:
    The getText() and isDisplayed() methods are not implemented in the Element class of Flutter Driver. Instead, you can use the getTextFinder() and waitFor() methods to retrieve the text of an element and check if it is visible.
final element = find.byValueKey('my_element_key');
final textFinder = find.descendant(of: element, matching: find.text('my_text'));

await driver.waitFor(element);
final text = await driver.getText(textFinder);
print(text);

await driver.waitFor(element);
final isVisible = await driver.waitFor(element).isPresent;
print(isVisible);
  1. Finding Multiple Elements:
    To find multiple elements, you can use the findAll() method, which returns a list of Element objects that match a given Finder:
final elements = await driver.findAll(find.byValueKey('my_element_key'));

This will return a list of all elements that have a ValueKey with the value 'my_element_key'.

  1. Getting Element Attributes:
    You can use the getDiagnostics() method of an element to retrieve its attributes. This method returns a Future<String> that contains a string representation of the element and its properties.
final element = find.byValueKey('my_element_key');
final diagnostics = await driver.getDiagnostics(element);

print(diagnostics);

This will print out a string representation of the element and its attributes, including its type, size, and location on the screen. You can also use regular expressions to extract specific attributes from the diagnostic output.

final element = find.byValueKey('my_element_key');
final diagnostics = await driver.getDiagnostics(element);

final regex = RegExp(r'width=(\d+)');
final match = regex.firstMatch(diagnostics);
final width = match.group(1);

print('Element width is $width');

This code extracts the width attribute of the element from the diagnostic output and prints it to the console.

my appiumdriver don't have findAll() api? and i use the getText() api if the element doesn't exist the code will run forever,can't stop.

this is my code:

public static void main(String[] args) {
AppiumDriver flutterDriver;
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Galaxy Note20 5G");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("noReset", true);
capabilities.setCapability("automationName", "Flutter");
try {
flutterDriver = new AndroidDriver(new URL("http://127.0.0.1:4723"), capabilities);
flutterDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
appiumDriver = new AndroidDriver(new URL("http://127.0.0.1:4723"), appiumCapabilities);
FlutterFinder finder = new FlutterFinder(flutterDriver);
Thread.sleep(10000);
FlutterElement element = finder.byText("App 反饋11");
String text = element.getText();
System.out.println(text);
} catch (IOException e) {
throw new RuntimeException(e);
}

@ashwithpoojary98
Copy link
Owner

@yyuanmeme attach the Appium logs

@yyuanmeme
Copy link

@yyuanmeme attach the Appium logs

2023-09-19 06:34:32:805 [FlutterDriver@c847 (0f49e93a)] Cached the protocol value 'W3C' for the new session 0f49e93a-d5ea-4c85-a0ba-a03400fc0281
2023-09-19 06:34:32:806 [FlutterDriver@c847 (0f49e93a)] Responding to client with driver.createSession() result: {"capabilities":{"subcommand":"server","address":"127.0.0.1","logFile":"D:\appium2.log","port":4723,"extraArgs":[],"allowCors":false,"allowInsecure":[],"basePath":"","callbackPort":4723,"debugLogSpacing":false,"denyInsecure":[],"keepAliveTimeout":600,"localTimezone":false,"loglevel":"debug","logNoColors":false,"logTimestamp":false,"longStacktrace":false,"noPermsCheck":false,"relaxedSecurityEnabled":false,"sessionOverride":false,"strictCaps":false,"useDrivers":[],"usePlugins":[],"tmpDir":"D:\Users\j_yuan\AppData\Local\Temp","platformName":"ANDROID","appActivity":"com.ctrip.ibu.myctrip.main.module.home.IBUHomeActivity","appPackage":"ctrip.english.debug","automationName":"Flutter","deviceName":"Galaxy Note20 5G","noReset":true,"fullReset":false,"fastReset":false,"skipUninstall":true}}
2023-09-19 06:34:32:810 [HTTP] <-- POST /session 200 13730 ms - 859
2023-09-19 06:34:32:810 [HTTP]
2023-09-19 06:34:32:889 [HTTP] --> POST /session/0f49e93a-d5ea-4c85-a0ba-a03400fc0281/timeouts
2023-09-19 06:34:32:889 [HTTP] {"implicit":30000}
2023-09-19 06:34:32:890 [FlutterDriver@c847 (0f49e93a)] Calling AppiumDriver.timeouts() with args: [null,null,null,null,30000,"0f49e93a-d5ea-4c85-a0ba-a03400fc0281"]
2023-09-19 06:34:32:891 [FlutterDriver] Executing Flutter driver command 'timeouts'
2023-09-19 06:34:32:892 [FlutterDriver@c847 (0f49e93a)] W3C timeout argument: {"implicit":30000}}
2023-09-19 06:34:32:892 [FlutterDriver@c847 (0f49e93a)] Set implicit wait to 30000ms
2023-09-19 06:34:32:894 [FlutterDriver@c847 (0f49e93a)] Responding to client with driver.timeouts() result: null
2023-09-19 06:34:32:895 [HTTP] <-- POST /session/0f49e93a-d5ea-4c85-a0ba-a03400fc0281/timeouts 200 6 ms - 14
2023-09-19 06:34:32:895 [HTTP]
2023-09-19 06:34:42:949 [HTTP] --> POST /session/0f49e93a-d5ea-4c85-a0ba-a03400fc0281/element/eyJ0ZXh0IjoiQXBwIOWPjemlizExIiwiZmluZGVyVHlwZSI6IkJ5VGV4dCJ9/click
2023-09-19 06:34:42:949 [HTTP] {"id":"eyJ0ZXh0IjoiQXBwIOWPjemlizExIiwiZmluZGVyVHlwZSI6IkJ5VGV4dCJ9"}
2023-09-19 06:34:42:952 [FlutterDriver@c847 (0f49e93a)] Calling AppiumDriver.click() with args: ["eyJ0ZXh0IjoiQXBwIOWPjemlizExIiwiZmluZGVyVHlwZSI6IkJ5VGV4dCJ9","0f49e93a-d5ea-4c85-a0ba-a03400fc0281"]
2023-09-19 06:34:42:953 [FlutterDriver] Executing Flutter driver command 'click'
2023-09-19 06:34:42:954 [FlutterDriver] >>> {"command":"tap","text":"App 反饋11","finderType":"ByText"}

blocked here

@yyuanmeme
Copy link

After 10 minutes I got the error message:
org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.7'
Driver info: io.appium.java_client.AppiumDriver
Command: [9c52e412-e35e-405a-b81e-599351976e53, clickElement {id=eyJ0ZXh0IjoiQXBwIOWPjemlizExIiwiZmluZGVyVHlwZSI6IkJ5VGV4dCJ9}]
Capabilities {appium:address: 127.0.0.1, appium:allowCors: false, appium:allowInsecure: [], appium:appActivity: com.ctrip.ibu.myctrip.main...., appium:appPackage: ctrip.english.debug, appium:automationName: Flutter, appium:basePath: , appium:callbackPort: 4723, appium:debugLogSpacing: false, appium:denyInsecure: [], appium:deviceName: Galaxy Note20 5G, appium:extraArgs: [], appium:fastReset: false, appium:fullReset: false, appium:keepAliveTimeout: 600, appium:localTimezone: false, appium:logFile: D:\appium2.log, appium:logNoColors: false, appium:logTimestamp: false, appium:loglevel: debug, appium:longStacktrace: false, appium:noPermsCheck: false, appium:noReset: true, appium:port: 4723, appium:relaxedSecurityEnabled: false, appium:sessionOverride: false, appium:skipUninstall: true, appium:strictCaps: false, appium:subcommand: server, appium:tmpDir: D:\Users\j_yuan\AppData\Loc..., appium:useDrivers: [], appium:usePlugins: [], platformName: ANDROID}
Element: [io.github.ashwith.flutter.FlutterElement@9f09019e -> unknown locator]
Session ID: 9c52e412-e35e-405a-b81e-599351976e53

the appium log is :
2023-09-19 06:44:19:234 [HTTP] <-- POST /session/9c52e412-e35e-405a-b81e-599351976e53/element/eyJ0ZXh0IjoiQXBwIOWPjemlizExIiwiZmluZGVyVHlwZSI6IkJ5VGV4dCJ9/click - - ms - -
2023-09-19 06:44:19:239 [HTTP]

@ashwithpoojary98
Copy link
Owner

The text value "App 反饋11" looks like localized Chinese. Is it possible to convert it to UTF-8 format?

String value = "App 反饋11";
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
String utf8EncodedString = new String(bytes, StandardCharsets.UTF_8);

@yyuanmeme
Copy link

The text value "App 反饋" can be found,but "App 反饋11"text is not exitst ,So it will keep searching。
I set the wait time ,but it doesn't work? --flutterDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
If the element cannot be found,Need to wait 10 minutes to get the failure reason?

@ashwithpoojary98
Copy link
Owner

@yyuanmeme Yes, if the locator is not correct, it may fail to find the element. Let me check on my end to reproduce the issue.

@baselalali
Copy link

Which driver are you using?

await driver.waitFor(element);

My appium driver doest not suggest waitFor(element); and getText()
@harshhhh15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants