I am testing my mobile app using Appium
. When app starting it directly navigation to HomeActivity
sometimes(if user already logged in). If user not logged in then opening LoginActivity
.
So, how can I know which activity
starting? so that I can execute logout code accordingly.
This code not executing at all because app navigation to HomeActivity
directly instead of LoginActivity
.
public void DemoTest()
{
AndroidElement editElement1 = driver.FindElementById("input_name");
editElement1.Clear();
editElement1.SendKeys("ak@ct");
AndroidElement editElement2 = driver.FindElementById("input_password");
editElement2.Click();
editElement2.SendKeys("qa");
driver.HideKeyboard();
driver.Manage().Timeouts().ImplicitWait =TimeSpan.FromSeconds(4);
AndroidElement editElement3 = driver.FindElementById("btnLogin");
editElement3.Click();
}
If you always want to test in fresh app, you can add noReset = false capability in your DesiredCapabilities.
If you want to check which screen is visible, there must be unique element or id in the homeActivityScreen and LoginActivityScreen. Then you can use isDisplayed() function.
If you use Page Object Model it will be very easy for you to check which screen is Display.
Similarly you can add page object model for other screen. Then in your test class you can check which screen is visible like
You'll need a way to identify the page as being the home page or login page. If you have access to the mobile app source code, you could give your home page a "homePage" AutomationId and your login page a "loginPage" AutomationId. If that is not possible, ask the developer to add them for you. Use these Id's in your appium code to uniquely identify the page that got loaded.
In your appium code, these are the steps you need to take:
Start your app:
driver = new AndroidDriver<AndroidElement>(url, cap);
Give the app some time to get loaded.
Check what page you are on, e.g. by checking the AutomationId or a other unique value for those pages.
In your test script, you can do it like this:
As Suban Dhyako already suggested, a page object design pattern is a very good practice. It keeps your code clean and clear to read. You can read more about it here.