How to capture error message text in appium - java

Error message is against EditText field. We don't have any direct locator available which points to error message. Can anyone suggest what alternate we have to handle this in appium.

Try this method if you are not able to assert Error message against EditText field.
Trigger text message on the screen
Capture screenshots
Convert image to text file
def assettoast(string)
sname = (0...8).map { (65 + rand(26)).chr }.join
$driver.driver.save_screenshot("#{sname}")
#Make sure tesseract is installed in the system. If not you can install using "brew install tesseract" in mac
system ("tesseract #{sname} #{sname}")
text_file="#{sname}.txt"
var= get_string_from_file(string, text_file)
raise if var != true
end
Check whether error message is there in text file
def get_string_from_file(word, filename)
File.readlines(filename).each do |line|
return true if line.include?(word)
end
end

There is no you will get using uiautomatorviewer and Google's original UiAutomator. Check old thread
However you may try Appium own implementation UiAutomator2 and Appium desktop inspector, maybe it will parse error.
Otherwise there is new appium end-point to compare images:
/session/:sessionId/appium/compare_images
You can use it to verify the error on screen

Related

Appium - the previously found element is not in the current view anymore (but an identical one is)

I'm testing an app where, after logging in, you create and confirm a six-digit pin. I'm just using the pin 123456 for testing. This block gets run twice:
final String PIN = "123456";
for(char c : PIN.toCharArray()){
driver.findElementByXPath("//XCUIElementTypeKey[#label='"+c+";]").click();
Thread.sleep(50);
}
when confirming the pin (i.e., the second time the block runs), I get the following error:
org.openqa.selenium.StaleElementReferenceException: The previously found element ""1" Key" is not present in the current view anymore. Make sure the application UI has the expected state. Original error: No matches found for Descendants matching type Key from input {(
Application, pid: 69030
)}
Presumably, Appium is caching the internal element ID then trying to use it again. Is there a way to disable this behaviour?
This happens on iPhone 8 but not iPhone 7. Doesn't happen on any of the Android phones I have.

Popup messages with selenium webdriver

I'm testing a website with validation etc. when the user types something incorrectly in each field and clicks save, a popup with the information appears. All those information are in the functions in the code (there is no JSON) file with them. My question is, how can I test them with selenium so when I'm writing a test script (in Java), so when the script is running they also show up on the console? I hope I explained my problem well.
Get Text From Pop Up:
String popUpText = driver.switchTo().alert().getText();
Accept Pop up Message:
driver.switchTo().alert().accept();

Printing in android studio - folding sections

When I was printing java code in Eclipse, the way of printing was WYSIWYG. So if I folded some selected sections (for example import section, but also any other foldable section), it was printed as folded.
In Android Studio code is printed allways fully unfolded. Does anybody know some way (plugin or some preferences setting etc.), how to set it to print also in the folding WYSIWYG style?
My problem is that I have done the printing part in android device when the printer is already configured on the device.
If the printer is not configured on the device, the exception occurs can you tell that how u know that printer is configured on device
e1=(EditText) findViewById(R.id.editText);
String name=e1.getText().toString();
PrintManager printManager=(PrintManager) this.getSystemService(Context.PRINT_SERVICE);
//PrintDocumentAdapter printAdapter = view.createPrintDocumentAdapter();
//Toast.makeText(getApplicationContext(),"hi nikhil"+name+""+printManager.getPrintJobs(),Toast.LENGTH_LONG).show();
String text=this.getString(R.string.app_name)+"Document";
// printManager.getPrintJobs();
printManager.print(text, new MyPrintDocumentAdapter(this), null);
Log.d("print", "print" + printManager);
on Android-Studio 4.0, I noticed that you can select the parts of a file you wish to print, and in the print dialog box, it will allow you to just print the selection. This isn't exactly what you're looking for, but it is a way to bypass the long lines of import statements and comments at the head of a file.

How to assert a screen with Sikuli java API(Not Sikuli script)

In Sikuli script, we have different ways of asserting like Screen.exists(a.png) etc for image assertion. But can someone please tell me how to assert an application screen with a screenshot file.
My requirement is that, I'm logging into a windows application, and after logging in I want to verify the screen that appears with a screenshot file(.png) captured earlier.
Sikuli api 1.0.2
Windows 7 64 bit
This should help you-
int timeout = <Specify timeout in millis>;
String imagePath = <give the path of your screenshot image here>;
ImageTarget target = new ImageTarget(new File(imagePath));
ScreenRegion result = desktop.wait(target,timeout);
Here if result is null after the timeout, it would mean the image found in the application does not match the one at the path of the screenshot you would have mentioned.

Bring the Firefox Browser to Front using selenium Java (Mac OSX)

I am using three instances of fire fox driver for automation.I need to bring current active firefox browser into front, Because I am using some robo classes for some opertation. I had tried java script alert for google chrome in mac ( same operation) and its worked fine. In windows used user32 lib. In the case of firefox mac its showing the alert in background but the web page is not come into front.
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
The above code I used for chrome in Mac. Same code is working and showing alert for firefox but the window is not coming to front.
Please suggest if there any other method for doing the same in firefox.
Store the window handle first in a variable, and then use it to go back to the window later on.
//Store the current window handle
String currentWindowHandle = this.webDriver.getWindowHandle();
//run your javascript and alert code
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
//Switch back to to the window using the handle saved earlier
this.webDriver.switchTo().window(currentWindowHandle);
Additionally, you can try to maximise the window after switching to it, which should also activate it.
this.webDriver.manage().window().maximize();
Try switching using the window name:
driver.switchTo().window("windowName");
Alternatively, you can pass a "window handle" to the switchTo().window() method. Knowing this, it’s possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Based on the Selenium documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp
As described in other topics, you can use
driver.manage().window().setPosition(new Point(-2000, 0));
too.
# notifications for selenium
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
current_path = os.getcwd() # current working path
chrome_path = os.path.join(current_path, 'chromedriver')
browser = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
browser.switch_to.window(browser.current_window_handle)
browser.implicitly_wait(30)
browser.maximize_window()
browser.get("http://facebook.com")
Only thing that worked for me on mac: self.driver.fullscreen_window().

Categories