How to assert the activity is finished with espresso [duplicate] - testing

This question already has answers here:
Espresso how to test if Activity is finished?
(8 answers)
Closed 12 months ago.
I want to assert the activity is finished after clicking a button.
eg: if the user clicked button with id R.id.finish, then the activity is finished.
I'm trying to look up apis with ActivitySenerio and Espresso, but none of these helps.
Ref:
https://developer.android.google.cn/training/testing/espresso

Without ActivitySenerio:
try {
onView(withId(R.id.finish)).perform(click());
} catch (Exception e) {
// assert the activity is finished
assertEquals(NoActivityResumedException.class, e.getClass());
}
With ActivitySenerio(Rule):
activity.findViewById(R.id.finish).performclick();
assertTrue(activity.isFinishing());

Related

Kotlin - delay between UI updates

I have created a simple tik-tac-toe game and have some bots you can play with. I am trying to have a delay between consecutive bot turns as otherwise all sequential bot moves appear on the screen at the same time
here is what I have tried in my code:
// bot functionality
private fun botTurn(botDifficulty: Int) {
var botMove = 100
when (botDifficulty) {
1 -> botMove = easyBot(confirmedMoves)
2 -> botMove = mediumBot(activePlayer, confirmedMoves, maxPlayers)
3 -> botMove = hardBot(activePlayer, confirmedMoves, maxPlayers)
else -> Toast.makeText(this, "What sort of bot is this??", Toast.LENGTH_SHORT).show()
}
trueCellID = botMove
// colour chosen segment
setSegmentColor(trueCellID, -1, activePlayer)
// TODO add 1-2 second delay after confirming move?
Log.d("Wait", "start of wait $activePlayer")
Thread.sleep(1000)
Log.d("Wait", "end of wait $activePlayer")
confirmMove()
}
using Thread.sleep seems to just delay all the UI updates until after all botPlayer sleeps have happened. I have also tried using Handler.postDelayed, GlobalScope.launch with a delay block and runOnUiThread with a SystemClock.sleep(1000)- these have the same problem of doing all the bot waiting, then the UI updating.
I even tried to adapt this solution a try - how to wait for Android runOnUiThread to be finished? but got the same result - big delay then all the UI updates.
Is there a fix for this or have I missed something simple?
As broot suggested in the comments, putting both setSegmentColor() and confirmMove() inside the postDelayed() block achieved the desired delay between botTurn()
val botDelay = 1500L
Handler().postDelayed(
{
// colour chosen segment then save move
setSegmentColor(botMove, -1, activePlayer)
confirmMove(botMove)
},
botDelay
)

Selenium WebDriver - Chrome issue [duplicate]

This question already has an answer here:
Best way to keep track and iterate through tabs and windows using WindowHandles using Selenium
(1 answer)
Closed 2 years ago.
Im wokring on a tool development that tests various services on Chrome browser. For each service I would need to launch a new tab on Chrome. I use the below code,
The first 2 new tabs works fine, when the 3rd tab is launched, the 1st tab's url is navigated to the one used for 3rd tab.
Not sure what's messing up. Need assistance!
I believe when your loop enters second iteration, your if statement executes to true.
You can just do following:
for(String actual: handles){
driver.switchTo().window(actual);
}
After execution of the above loop, your script will always point to last window opened.
Then you can do:
driver.get(URL + service + URL_);
outside the loop.
P.S.: If required, for more enhancement you can use below script:
private void switchToLatestBrowserWindow(WebDriver driver, Set<String> priorHandles)
{
new WebDriverWait(driver, 60).until( // keep trying to switch for 60 seconds
d -> {
Set<String> newHandles = d.getWindowHandles();
if (newHandles.size() != priorHandles.size()) {
log.Info("NewHandles Size not equal to PriorHandles size.");
String lastWindowHandle = "";
for (String windowHandle : newHandles) {
lastWindowHandle = windowHandle;
}
log.Info("LastWindowHandle Id: " + lastWindowHandle);
d.switchTo().window(lastWindowHandle);
log.Info("Switched window to " + lastWindowHandle);
return true;
} else {
return false;
}
});
}
priorHandles is set of window handles retrieved before opening new window. (the first line of code, in your case). So complete snippet would look like:
Set<String> priorHandles = driver.getWindowHandles();
((JavasriptExecutor) driver).executeScript( script: "window.open()");
switchToLatestBrowserWindow(driver, priorHandles);
driver.get(URL + service + URL_);

Unable to validate the visibility of the notification message using explicit wait

I want to validate whether a notification message appears on the screen upon successful save.
The Problem here is the notification message disappears in few seconds(less than 5 sec).
I tried the following code to validate whether notification message appears or not:
new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[contains(text(),'Notification Message')]")));
}
catch(Exception e) {
System.out.println("The Exception is ::::::::::::: "+e.getMessage());
return false;
}
return true;
Additionally I also tried the following
new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//h2[contains(text(),'Notification Message')]")));
In both the cases Exception is thrown though the notification message appears.
Please recommend if any other way to validate the notification message.

Wait for a webelement to appear and keep clicking until that element appears

I am stuck in a peculiar logic.
My requirements are as follows
Save the information
Click on Refresh button
Wait for inquiry id to appear
If(3) is not true wait for 10 seconds and trigger Step(2) again
Keep 4 in loop unless the inquiry id appears
The inquiry id is of six digits so i am using
WebElement getInquiryId=driver.findElement(By.xpath("//a[contains(text(),'^\d{6}$')]"));
Please help me out in framing the selenium code based on following requirements
This would somehow be your code for Step 4:
while(!getInquiryId.isDisplayed()){
try {
Thread.sleep(10000); //wait for 10 seconds
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(getInquiryId.isDisplayed()){
break;
}else{
refreshButton.click();
}
}

Alert Handling + Java + webdriver [duplicate]

This question already has answers here:
Selenium WebDriver with Java: Can't accept alert
(3 answers)
Closed 9 years ago.
I am trying to use switch to alert and perform an action but i face error.
Now the real issue is when i put the below code in try,catch it works perfectly. i mean it handles the alert perfectly. But when i use the same without try, catch code it throws the below exception
Alert alert = driver.switchTo().alert();
String AlertText = alert.getText();
System.out.println(javascriptconfirm.getText());
alert.accept();
Please find the error below
No alert is present (WARNING: The server did not provide any stacktrace information)
The idea is when you deal with alerts you have to check whether alert is present first.
I would use this approach:
public boolean isAlertPresent() {
boolean presentFlag = false;
try {
// Check the presence of alert
Alert alert = driver.switchTo().alert();
// Alert present; set the flag
presentFlag = true;
// if present consume the alert
alert.accept();
} catch (NoAlertPresentException ex) {
// Alert not present
ex.printStackTrace();
}
return presentFlag;
}
here you can get details Also do not forget about debug step by step to get to know on what step alert appears/not appears.
Hope this helps you.