I am trying Yii Selenium unit testing. How can I manage confirm box in that?
Which method is used to get alert message text?
PHPUnit_Extensions_SeleniumTestCase provides
void assertAlertPresent() Reports an error if no alert is present.
void assertNoAlertPresent() Reports an error if an alert is present.
For managing alerts, you can use methods from Selenium API
http://release.seleniumhq.org/selenium-core/1.0/reference.html
storeAlert()
chooseCancelOnNextConfirmation()
chooseOkOnNextConfirmation()
P.S. Selenium tests are functional, not unit
Related
I have a scenario where I have to hover over a menu link and click on the drop down sub menus. The code that I'm using is below:
public void changeLanguageTest() throws InterruptedException
{
WebElement LanguageMenu = driver.findElement(By.cssSelector(".change-language>button"));
action.moveToElement(LanguageMenu);
WebElement mongolianLang = driver.findElement(By.cssSelector(".change-language>ol>li:nth-child(3)>a"));
action.moveToElement(mongolianLang).click().build().perform();
Thread.sleep(3000L);
}
But when I run this code, It fails with an error message :-
org.openqa.selenium.UnsupportedCommandException: POST
/session/3077f893-d9ab-487d-b09f-c5bcd135ea31/moveto did not match a
known command
I tried below mentioned code too but no success and same error occurred.
BaseClass.action.moveToElement(LanguageMenu).moveToElement(mongolianLang).click().build().perform();
I'm using webdriver v2.53 and running it on FF v47.0.1.
As a bug logged here geckodriver does not yet implement actions. The actions we will implement are those being defined right now in the W3C WebDriver standard and not those of Selenium.
Selenium has said they will provide a Selenium-to-W3C-WebDriver shim for actions, but this may take some time to produce after we have made our implementation. Implementation in geckodriver/Marionette has not yet started.
As mentioned here from v0.12.0 of geckodriver, Implemented routing for new actions API, but it too is not yet fully implemented in Marionette
You should upgrade your geckodriver.
I know that for using my js file I must use Selenium Core extension but I cannot understand what Selenium IDE Extension field is for?
Thanks in advance.
Selenium extensions provide a way to add more functionality/feature to Selenium as per your requirements. This is also known as Selenium User-Extensions and Selenium Custom-Extensions.
The concept is pretty simple, extend Selenium by adding your own actions, assertions and locator-strategies. Add JavaScript methods to the Selenium object prototype and the PageBot object prototype. On startup, Selenium will automatically look through methods on these prototypes, using name patterns to recognize which ones are actions, assertions and locators.
User-Extensions can be used with Selenium IDE (see this) and Selenium RC (see this).
Do not get confused with different names. Same concept is getting used at different places, differently.
"Selenium IDE Extensions" is mainly used to extend the recording behavior of Selenium IDE.
To use the extensions
Copy the extension code into a new .js file. You can place it
anywhere on your disk.
Open Options - Options... in the menu bar.
Choose the saved file in "Selenium IDE extensions" field and click OK.
Restart Selenium IDE by closing the window and opening it again.
Example
By default, Selenium IDE only records click events on certain types of elements (e.g. , , ...).
You can record any click events occured in a page by putting the following code as Selenium IDE extension.
Recorder.removeEventHandler('clickLocator');
Recorder.addEventHandler('clickLocator', 'click', function(event) {
if (event.button == 0) {
this.clickLocator = this.findLocator(event.target);
}
}, { capture: true });
I'm pretty new to Thucydides / JBehave, but haven't found any solutions posted to this one. This is the first time I've used Thucydides / JBehave, but have used Selenium before.
I have my .story file. The .story file lists 5 scenario's.
Each scenario is implemented in it's own POJO java class.
e.g
public class ManagerBypassLoginSteps
{
#Steps
ManagerSteps managerSteps;
#Given("the manager is not logged in")
public void mangerLogsIn()
{
managerSteps.start();
}
#When("the manager goes to a different page")
public void managerToDashboardPage()
{
managerSteps.goesToDashboardPage();
}
#Then("they should see the login page")
public void managerShouldSeeLoginPage()
{
managerSteps.verifyOnLoginPage();
managerSteps.close();
}
}
The ManagerSteps class extends net.thucydides.core.steps.ScenarioSteps, but is otherwise just a POJO. The ManagerSteps calls the page objects - all very normal stuff and as per the examples.
In the scenario above, the test fails as the code displays an error message instead of returning the user to the log in page - that's fine, the security code doesn't meet the specified requirements (yet) But, after the assert inside the ManagerSteps class fails, the test appears to stop. This means I have a browser instance just sitting there until I close it. When run as a series of tests, this means a broswer in the Selenium grid is now tied up.
I need a way to detect a test failure, to call to the page object to close / quit.
I can't find the equivalent of a #Before or #After that will always run, I could find it I could use the #After to close the page object's webDriver.
I am not using Thucydides to manage the WebDriver lifecycle as I couldn't find any examples of this that did not use the ThucydidesRunner.class.
Do I have to use the ThucydidesRunner.class ? My impression from the Thucydides manual for integrating with JBehave (http://thucydides.info/docs/thucydides/_writing_acceptance_tests_with_jbehave.html) didn't suggest it was required.
How does everyone else handle test failures in this situation ?
The real problem I had was closing the browser, after a test failure.
The best way to manage the WebDriver appear to be to use the Thucydides #ManagedDriver annotation to handle the lifecyle.
I ended up using a very simple JUnit runner like so
public class UserLogin extends ThucydidesJUnitStory
{
#Managed
public WebDriver webdriver;
#ManagedPages(defaultUrl = "http://localhost:8080/dc-bus-main-web")
public Pages pages;
}
When this is started, it maps to the user_login.story file, through JBehave's naming convention, then starts normally.
My company wants me to develop a "Visual" GUI style BDD function using JBehave and Selenium, which uses javascript alert/confirm popup window to prompt user what is the exact step the running test reaches, eg:
Given I goto "www.google.com"
When I login
So we want to add Javascript alert window to popup during the automation test, the popped up window has the "OK" button, so when user click the OK button, the test will continue to the next step, and so on...
My issue is: I wrote a javascript func using Selenium's executeScript API which invoke the pop up alert window:
public void stepText(String step) {
executeScript("alert('"+step+"');");
}
So I expect when I click the OK button, the popped up window will disappear and test will continue to next step... But what shocked me is that when I click it, the test throw exception and crashed...
The exception is: selenium.WebDriverException
But I found if I add the following code to make the test automatically detect the alert window and accept it by using the following usual selenium alert handle function:
Alert alert=switchTo().alert();
alert.accept();
This can make the test runs well, so it looks I can NOT manually click the alert (after I manually click, the selenium still can NOT go back to the browser...lost connection to browser?), but the automation alert handle code works...
Of course, we want to let user to manually to click alert window to control the test execution, not the automation handle alert.
I really got stuck here for a while, and did a lot googling to search, but can not find similar example online, I hope you can shed me light on it, since you are much more guru than me on JBehave and Selenium.
I will be much grateful if you can help me out.
Selenium is a browser automation tool, it does not anticipate user's interactions.
Therefore, I'd use a simple Java GUI window to present the user with messages/options. Afterall, you are testing a web application in a browser, but the program itself is Java and has nothing to do with the browser. A usual Swing option dialog should be enough.
JOptionPane.showMessageDialog(null, "Login successful.");
String loginAs = JOptionPane.showInputDialog("Login as:", "admin");
int choice = JOptionPane.showConfirmDialog(null, "Use production data?");
(note that you don't want to invoke this in the EventQueue.invokeLater() block, because you want the dialogs to be blocking)
This way, you won't interact with Selenium or the browser in any way, you won't confuse it and you'll get the user input cleanly.
That said, if you insist on using alerts, I think it's definitely doable, but as of now (June 2013, Selenium 2.33.0), I don't know how:
The issue is not reproducible on IE8. After the executeScript("alert('Something.')"); call, Selenium waits for the call to return something and then proceeds normally. So you're good on IE.
However, with FF21, Selenium fails immediatelly with UnhandledAlertException just as you said.
I tried two obvious solutions:
js.executeScript("alert('something')");
new WebDriverWait(driver, 10)
.pollingEvery(100, TimeUnit.MILLISECONDS)
.ignoring(UnhandledAlertException.class)
.until(ExpectedConditions.not(ExpectedConditions.alertIsPresent()))
.wait();
and
js.executeScript("alert('something')");
boolean alertVisible = true;
while (alertVisible) {
try {
driver.switchTo().alert();
} catch (NoAlertPresentException ignored) {
alertVisible = false;
}
}
Both make FF fail horribly with an internal JavaScript exception. Possibly a bug that might get fixed (please test it, check whether it had been reported and report it if you're interested in it), so I'll leave the solutions here for future generations.
But as I said before, it's possible that it won't get fixed, since Selenium doesn't count on manual user interactions.
Not sure how this behaves in other browsers.
I am trying to handle alerts using Selenium Webdriver, according the selenium documentation the correct implementation is:
Alert alert = driver.switchTo().alert();
However I am getting error message The method alert() is undefined for the type WebDriver.TargetLocator
what version of Selenium 2 has this web driver api
Alerts are not be supported by HmltUnit driver. In HtmlUnitDriver.java (Selenium 2.0.6) :
public Alert alert() {
throw new UnsupportedOperationException("alert()");
}
Which driver are you using?
I'm using selenium 2.3 and it works for me. From what I've seen, alerts only work with windows that ONLY have the ok button. Thats where you put the alert.accept(); statement in. Another problem that I've seen with alerts is that it may jump over the alert so you may have to have a wait statement for that specific alert. Hope this helps.
You may need to use a separate import statement for Alert:
import org.openqa.selenium.Alert;
I had the same issue.