How can I testing the Message Dialog/Box from SWT(with SWTBot)? - java

Is the message box native SWT? I ask, because SWTBot can't handle native SWT dialogs like message boxes or file dialogs.
Suggestion: work in your application code with (JFace) MessageDialog.openInformation(....) and not nativ SWT dialogs. This works fine with SWTBot.<
I can with MessageDialog.openInformation(....) create a MessageDialog, but I will testing what happens when I click "Yes" or "No".
I tried to interpreted the "Yes" and "No" Buttons in der MessageDialog as Buttons.
okButton = bot.button("Yes");
However I get a "org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException: Could not find widget."
Who I can test it?

You cannot test the native dialog with SWTBot.
Reference: SWTBot Wiki

Related

How to create a Lottie Alert Dialog in Java

I've tried searching for the Java tutorial regarding creating my LottieAlertDialog, but I can't find any. Everywhere it's in Kotlin, but I need the Java code as my project is in Java.
I've tried creating my LottieAlertDialog in this way:
LottieAlertDialog.Builder alert=new LottieAlertDialog.Builder(context,DialogTypes.TYPE_CUSTOM,
"social.json") //Here social.json is inside assets folder
.setTitle("Social")
.setDescription("social");
alert.build();
But the dialogbox doesn't show, when I run the app. To check whether my alert dialogbox was being created or not I tried testing it by printing the description set in the dialog in a Toast:
Toast.makeText(context,alert.getDescription(),Toast.LENGTH_SHORT).show();
The toast works and its showing "social"! That means the dialog is being created. But unfortunately it doesn't show in my app. What do I do? I've implemented all the dependencies as shown in the below link:
lottiealertdialog
Ok, after much hankering, I finally came to the solution. It's
alert.build().show();
The thing is not related to Kotlin or Java as such, you need to show the dialog once you have built it. So far your code is correct. You just need to show it further like this
LottieAlertDialog.Builder alert = new LottieAlertDialog.Builder(context, DialogTypes.TYPE_CUSTOM,
"social.json")
.setTitle("Social")
.setDescription("Social")
.build()
.show();

How to click on Cancel on a windows explorer that displays when a upload button is clicked on a web page using Selenium WebDriver with Java

I am very new to Selenium WebDriver with Java. There is a Upload button on a job portal. When I click on that button windows explorer is displayed to choose the file. there are open and cancel buttons on this window. i want to select the cancel button. since it is a windows explorer i cannot inspect the cancel button. how do we write the code for cancelling the button. Thanks in advance.
driver.get("https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US");
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(
By.xpath("//[#id='container']/div/div/div[2]/div/div/div[2]/div/div[1]/div/div[1]/button")).click();
I do not have Java programming skills. I hope the below python code is easy to translate. In my opinion, combining windows action with selenium calls is often flaky.
There is a hidden element called 'upload resume button', you can change the attribute value to see it on the UI, use the send keys method on the element to upload the resume.
from selenium.webdriver import Remote, DesiredCapabilities
driver = Remote(desired_capabilities=DesiredCapabilities.CHROME)
driver.get('https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US')
driver.execute_script(
"document.getElementById('upload-resume-button').setAttribute('class', '')"
)
upload_your_resume = driver.find_element_by_id('upload-resume-button')
upload_your_resume.send_keys(r'C:\test\resume.docx')
The above code worked in my local.
Short answer is you cant, but if you want to upload a local file you can use send_keys on the input field, sending the file path.
Here is a python exemple:
driver.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")
you can use Robot class to simulate native keyboard and mouse actions to interact with windows based pop-ups.
The shortcut to close any opened window is: “Alt + Space +C” – Closes the focused window.
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_ALT);
rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_SPACE);
rb.keyRelease(KeyEvent.VK_ALT);

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();

Control name in dialog box of class SunAwtDialog

Usually, my AHK program reads the content of the "Filename:" text zone in file dialog boxes (Open, Save As, etc.) using the following info: read the content of control "Edit1" in dialog box of class "#32770". It can also read the list of controls using this piece of code:
; in Notepad, open the "Open" dialog box
WinGet, strControlsList1, ControlList, ahk_class #32770
WinGetTitle, strTitle1, ahk_class #32770
MsgBox, , %strTitle1% controls, %strControlsList1%
I'm trying to do the same in Java programs implemented for Windows (PDF Split and Merge and Geogebra) using Java Access Bridge, I guess. I know that these programs use the class name "SunAwtDialog" instead of the usual "#32770" for their dialog boxes. But I can't get access to the "Filename" control name. "Edit1" does not work. And I can't get the list of controls in this dialog box using this code:
; in a Java app like PDF Split and Merge, open the "Save As" dialog box
WinGet, strControlsList2, ControlList, ahk_class SunAwtDialog
WinGetTitle, strTitle2, ahk_class SunAwtDialog
MsgBox, , %strTitle2% controls, %strControlsList2%
Any idea how an AHK script could get info from these Java apps dialog boxes?

Selenium Webdriver : Dismiss Chrome Alert

I want to select the check box Prevent this page from creating additional dialogs before selecting ok to close the alert
Currently i am using
Alert alert=driver.switchTo().alert();
//check the checkbox
alert.accept();
As an normal interactive user to check the check box i have to use a combination of <Tab> + <Space/Enter> keys. The <Tab> shifts the focus to the check-box and the <Space/Enter> checks the check-box.
Solutions Tried
I tried using Java sendKeys mechanisms ( Robot class, driver.sendKeys(), etc.), but an UnhandledAlertException is getting thrown.
I tried using alert.sendKeys which is different from driver.sendKeys() but it too failed
//check the checkbox
alert.sendKeys("\t");
alert.sendKeys("{TAB}");
alert.sendKeys("\uE004");
alert.sendKeys("\\U+0009");
alert.sendKeys(Integer.toString(KeyEvent.VK_TAB));
I am trying to avoid robot class as much as possible as i need to run the test in grid in which case robot class will not work.Any pointers on how to send keys to the alert window ?
Temporarily i could do it using a javascript window.alert = function() {}; by simply overriding the alert with an empty function just curious to know if it could be done with webdriver functions like alert.sendkeys or any other methods ??
Any help is greatly appreciated!!

Categories