is element displayed android appium automation java

时间:2019-05-19 04:03:16

标签: java android automation appium

first, i need to check if the element com.simplemobiletools.gallery:id/text_view is displayed within a timeframe of 2-3 seconds. Second, if it IS displayed, click the element android:id/button2, but if it ISNT displayed, keep running code.

What commands do I use to do this? Thank you in advance.

2 个答案:

答案 0 :(得分:0)

创建如下所示的方法:

public boolean isElementDisplayed(MobileElement el){
     try{
        return el.isDisplayed();
     }catch(Exception e){
        return false;
     }
}

然后您可以通过调用上述方法来检查元素是否显示:

MobileElement element = driver.findElementById('com.simplemobiletools.gallery:id/text_view');
boolean isElementVisible = isElementDisplayed(element);
if(isElementVisible){
   //click element
   driver.findElementById("android:id/button2").click();
}else{
   //element is not visible... continue Test

}
  

如果不使用try catch,则在找不到元素时会引发异常。

答案 1 :(得分:0)

a。等待元素3秒钟,并根据需要进行1秒钟或更短的轮询

b。如果发生异常,则表示该元素不会在3秒内显示

c。检查webelement是否不为null,然后仅单击它

void main() {

   runApp(MyApp());
   }

class MyApp extends StatelessWidget {
  MyApp();

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        StreamProvider<FirebaseUser>.value(
          stream: FirebaseAuth.instance.onAuthStateChanged, // Provider here
        ),
      ],
      child: MaterialApp(
        title: 'My App',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primaryColor: Colors.green,
          primarySwatch: Colors.green,
          accentColor: Colors.yellow,
        ),
        home: MainPage(),
      ),
    );
  }
}

class MainPage extends StatefulWidget {
  MainPage({Key key, this.storage}) : super(key: key);
  final FirebaseStorage storage;
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage>
    with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    final user = Provider.of<FirebaseUser>(context); // gets the firebase user
    bool loggedIn = user != null;

    return loggedIn ? HomePage() : LoginPage(); // Will check if the user is logged in. And will change anywhere in the app if the user logs in
  }
}