Pop up message to display for few seconds in selenium - java

I am running a selenium script using Java. The functionality of the code is to open a Google web page and exit the browser.
After exiting the browser i need a pop up message to be displayed saying "Code executed" and fade away in couple of seconds.
I believe there is an way to display a message either by using JQuery or Javascript but i do not know how to inject that JQuery/Javascript method into my script.
Here's my code:
public class test1 {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://google.com");
driver.quit();
//I need a method here to display a pop up msg and fade away
}
}

Add this line where ever you want to display the popup box.
JOptionPane.showMessageDialog(frame,"Welcome please look in to the process");
plese go through https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
Regards,
BK

Related

Selenium : How to click an element in the chat page whenever the new chat is available

I have a chat page and need to click the chat whenever the chat appears. I am using a basic main method and I used this code `public static void main(String[] args) {
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ravi\\Documents\\workspace\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://demo.com/#!/login");
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[#ng-model='form.username']")).sendKeys("michea#gmail.com");
driver.findElement(By.xpath("//input[#ng-model='form.password']")).sendKeys("143###");
//driver.findElement(By.xpath("//button[#type='submit']")).click();
List<WebElement> sea = driver.findElements(By.xpath("//div[#id='page']//div[#ng-click='selectSession(session)"));
}`
I know that list web elements is not a correct one and I know that I have use a for loop and if statement but I am so much confused..Please help me here Thank you in advance
First of all it's better to use TestNg or JUnit annotation #Test and some public void method to write the test in it instead of main.
And it is hard to give you some advice without more explanation from your side about page structure.
The only thing I can suggest add some if statement where you can check if the chat is displayed or not, if YES click on it.
if(!driver.findElements(By.id("chat_id")).isEmpty()){
driver.findElement(By.id("chat")).click();
}

Get Text from Auto Suggest Dropdown not working in Selenium [duplicate]

This question already has an answer here:
How to automate Google Home Page auto suggestion?
(1 answer)
Closed 3 years ago.
I am trying to verify if the selected value in an AutoSuggest Dropdown is correct using Selenium. But using the getText() doesn't return any results.
Below is my code
public class AutoSuggestDropdownsTest {
public static void main(String[]args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/resources/executables/chromedriver") ;
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.get("https://example.com/");
driver.findElement(By.xpath("//*[#class='stamp remove-sign']")).click();
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys("to");
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys(Keys.DOWN);
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys(Keys.DOWN);
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys(Keys.DOWN);
System.out.println(driver.findElement("//input[#id='or-search']")).getText());
System.out.println("hello");
driver.quit();
}
Usually autosuggestion results take some time to appear, so if you do the sendKeys(Keys.DOWN) immediately after sending the to it is very likely that no autosuggestions appeared yet, and this way the DOWN key has no effect. To check if this is the case (for development purpose of the test only) put a sleep 10s, and check if with the sleep the text you think should appear, actually appears.
if it does appear, then you should implement some kind of smart waiting for the results to appear.
if it doesn't appear, maybe there is another problem, in which case it would be very useful if you could post the html of the page after the autossugestions appeared.

Handling an AngularJS popup with Selenium WD

The following picture shows a pop-up that appears when clicked on the "Random User" text; WD is not able to shift its focus to the pop-up you see.
Below is the code I employed with various trial codes commented as in
public static void teardownAppRetrieveOnce() throws InterruptedException {
synchronized(obj){
obj.wait(2000);
driver.findElement(By.xpath(".//*[#id='sidebar']/nav/ul[2]/li[3]/button[1]/span/span")).click();
System.out.println("Clicked sidebar..");
obj.wait(2000);
String winHandleBefore = driver.getWindowHandle();
**driver.switchTo().window(winHandleBefore);
driver.switchTo().findElement(By.id(".//*[#id='logoutButton']")).click();**
***driver.switchTo().activeElement().findElement(By.id(".//*[#id='logoutButton']")).click();***
System.out.println("Clicked on logout");
obj.wait(2000);
driver.switchTo().alert().accept();
obj.wait(2000);
System.out.println("Logged out...");
}
}
Please help me clarify if such pop-up means a separate new window or frame that is to be handled?
NOTE:
** means the first solution I tried that failed
*** means the second solution i tried that too failed
I have had a lot of trouble with WebDriver/AngularJS. Sometimes native Selenium methods of WebElements work - sometimes not. The best solution is to execute raw Javascript on the web page.
I your case, it would be something like this:
driver.executeScript("$x(\".//*[#id='logoutButton']\")[0].click()");

Having issue finding element Fluentlenium

I am writing some automated tests using Fluentlenium and PhantomJS. I am having trouble accessing the id "#title". The test I have written is as follows:
#Test
public void testCreateButton() {
startAppWithCallback(new F.Callback<TestBrowser>() {
public void invoke(TestBrowser browser) throws InterruptedException {
CalendarPage calendarPage = browser.createPage(CalendarPage.class);
calendarPage.withDefaultUrl(BASE_URL);
calendarPage.go();
calendarPage.selectCreateButton();
calendarPage.typeTitle("Java Fundamentals");
browser.await().atMost(3, TimeUnit.SECONDS);
}
});
}
The test is running, and seems to be able to select the Create button, which should then open up a modal window, but for some reason it is having trouble seeing the id on this modal. The error message that I get is as follows:
org.openqa.selenium.NoSuchElementException: No element is displayed or enabled. Can't set a new value.
Is there something I am not doing when it comes to accessing the id on the modal window? Any help at all would be much appreciated.
Usually modal windows take some time to attach to the DOM of the page you are accessing. Though you have added 3 seconds to wait for the element to appear/ attach to the DOM but the time is not sufficient. I would not recommend to increase the timeout but would recommend to wait until for the element to appear and then move forward. for e.g. you could do following thing to wait for an element to appear on the page instead of waiting statically:
FluentWaitMatcher matcher = page.await().atMost(, TimeUnit.SECONDS).until(findPattern);

The screen is not navigating even the click() method is executed successfully in mobile web app using selenium web driver

I was trying to click a button on my mobile web app, using selenium web driver. The button is located, the text over the button can be derived and even the click event is performing well. But the navigation doesn't occur.
I tried with Click() method, sendKeys() method and also with script executor. But couldn't process further on.
CODE:
public class TestWeb
{
WebDriver driver;
private Selenium selenium;
#Before
public void setUp() throws Exception {
driver = new IPhoneDriver();
driver.get("http://10.5.95.25/mobilebanking");
}
#Test
public void TC() throws Exception {
System.out.println("page 1");
Thread.sleep(5000);
WebElement editbtn1 = driver.findElement(By.id("ext-comp-1018"));
String s1 = editbtn1.getText();
System.out.println(s1);
editbtn1.click();
editbtn1.sendKeys(Keys.ENTER);
((JavascriptExecutor)driver).executeScript("arguments[0].click;", editbtn1);
System.out.println("ok");
}
#After
public void tearDown() throws Exception {
System.out.println("*******Execution Over***********");
}
}
I tried click, sendKeys and ScriptExecutor separately and also combined. It is executing without any error but the navigation doesn't occur.
Does anybody can help me with some other ways to perform click function on the button?
Ram
This may not be your issue but I noticed "ext-comp-" and guess you are using extjs.
I'm using GXT and while finding by id worked for many things, on some submit buttons it didn't.
I had to use firebug in firefox to locate the element and copy the xpath.
Then I could click the element by
driver.findElement(By.xpath("//div[#id='LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click(); // worked
It was failing silently for me too. My submit button has the id of LOGIN_SUBMIT so I don't know why the following failed but ....
driver.findElement(By.id("LOGIN_SUBMIT")).click();//failed
Edit:
Here is an exact example (case 1 of 2):
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']")));
//wait.until(ExpectedConditions.elementToBeClickable((By.id("gwt-debug-LOGIN_SUBMIT")))); <!-- id works as well
OK so the element is found. It will timeout and throw an exception if it is not.
Still, the following fails (under firefox, works with chrome) with no error and the page does not navigate.
driver.findElement(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']")).click();
//driver.findElement(By.id("gwt-debug-LOGIN_SUBMIT")).click(); <-- fails too
What I have to do is:
driver.findElement(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click();
So my experience was that even if I found the element with xpath, clicking failed unless I used a complete xpath.
Here is another exact example (case 2 of 2):
I can find an element like so:
WebElement we = driver.findElement(By.xpath("//*[#id=\"text" + i + "\"]"));
I know I have found it because I can see the text via:
we.getText();
Still selecting by the path I found it fails.
//get outta town man the following fails
driver.findElement(By.xpath("//*[#id=\"text" + i + "\"]")).click();
In this case there is not more explicit xpath to try as in case 1
What I had to do was use css:
//bingo baby works fine
driver.findElement(By.cssSelector("div#text" + i + ".myChoices")).click();
Actually, I obtained the css path via firebug than shortened it.
//this is what I recieved
html.ext-strict body.ext-gecko div#x-auto-0.x-component div#x-auto-1.x-component div#x-auto-3..myBlank div#choicePanel1.myBlank div.x-box-inner div#text3.myChoices //text3 is the id of the element I wanted to select
Whether or not you can figure out your needed xpaths and css selectors, I don't know, but I believe I experienced exactly what you did.

Categories

Resources