"Element is not currently visible and so may not be interacted" while trying to click on sub-menus - selenium

I am automating flipkart site. My aim is to login to site and logout from site.
I successfully logged in but unable to logout as the logout link is under sub-menus (mousehover).
Attached the screenshot.
I tried all possible scenarios like using Actions class and javascriptexecutor.
Using javascriptexecutor it is working fine only if I manually place cursor on the sub-menu otherwise it is throwing an error.

I had no problem with getting this to work.
Here is the script using the getting-started-with-selenium framework
#Config(url="http://flipkart.com", browser=Browser.FIREFOX)
public class TestFlikPart extends AutomationTest {
#Test
public void testLoginLogout() {
String username = "<username>";
String password = "<password>";
click(By.cssSelector("a[href*='/login']"))
.setText(By.cssSelector("input[name='email']"), username)
.setText(By.cssSelector("input[name='password']"), password)
.click(By.cssSelector("input[type='submit'][value='Login']"))
.validatePresent(By.cssSelector("li.greeting-link > a"))
.hoverOver(By.cssSelector("li.greeting-link > a"))
.click(By.cssSelector("ul.account-dropdown a[href*='/logout']"))
// should be logged out now.
.validatePresent(By.cssSelector("a[href*='/login']"));
}
}
I think MrTi is correct - i think you might have forgotten to do .perform() on the action. A further explanation on the hoverOver() method, this is what that method contains -
actions.moveToElement(driver.findElement(by)).perform();

Related

Can't login sites using Selenium Webdriver (Java)

Some reason it can't login to the website. After typing the user name and password, he clicks on the login button, but for some reason, when the login button is clicked, the required process does not work. It happen like the page has been refreshed.
public class main extends baseTest {
#Test
public void gittiGidiyor() throws Exception {
boolean logo = displayElement(By.cssSelector("[alt='GittiGidiyor']"));
if (logo) {
logger.info("Main Page.");
} else {
logger.info("not Main Page");
fail();
}
assertName(driver.getTitle(),"GittiGidiyor");
clickElement(By.xpath("(//*[#class='gekhq4-4 egoSnI'])[1]"));
Thread.sleep(5000);
clickElement(By.cssSelector("[class='qjixn8-0 sc-1bydi5r-0 kNKwwK']"));
sendElement(By.id("L-UserNameField"), "blabla#hotmail.com");
Thread.sleep(5000);
sendElement(By.id("L-PasswordField"), "blabla11");
Thread.sleep(5000);
clickElement(By.id("gg-login-enter"));
Thread.sleep(5000);
boolean account = displayElement(By.cssSelector("[title='Account'] [class='gekhq4-4 egoSnI']"));
if (account) {
logger.info("login successful.");
} else {
logger.info("login unsuccessful.");
fail();
}
clickElement(By.id("gg-login-enter")); This line should be work but i don't know why, it just doesn't.
Video
I've tried same thing with different site which ended exactly the same. But the solution I came up with is when the login page is opened I manually enter the login infos and check like this:
while(!account){
account = displayElement(By.cssSelector("[title='Account'] [class='gekhq4-4 egoSnI']"));
Thread.sleep(5000);
}
You need to add an Agent. You can use the URL below for the solution.
Changing the user agent using selenium webdriver in Java

Selenium: PayPal Login process in Web Application

My application having Paypal as a payment option, while doing automation testing using Selenium Web Driver, the process flow goes on without any error but when flow navigate to Paypal login page filling all the fields(username, password) and then click on login button, the EXPECTED FLOW Is: page redirects to order confirm page, but the Paypal login page get refresh and stay on the page with empty fields. I also checked it by increasing timeout seconds but nothing happens.
Below is my code:
#Test
public void testAsdChannel(){
for(RemoteWebDriver driver: drivers){
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
HomePage hPage = HomePage.navigateTo(driver, SERVER);
....
AsdConfirmPage asdPage = asdChannelFlow.acceptOffer();
asdChannelPage.paypalLogin("username", "pswd");
Assert.assertEquals(asdConfirmPage.asdOrderConfirm(), "Your order has been received");
}
}
// Here below i am providing paypalLogin(String userName, String pswd) Method:
public void paypalLogin(String username, String password){
paypalEmail.sendKeys(username); // 'paypalEmail'text field element on paypal login page
paypalPswd.sendKeys(password); // 'paypalPswd' field element on same.
paypalLoginBtn.submit(); // 'paypalLoginBtn' button element on same
}
// And here are the elements paypalLoginBtn, paypalPswd, paypalEmail
#FindBy(name = "email")
WebElement paypalEmail;
#FindBy(name = "password")
WebElement paypalPassword;
#FindBy(id = "loginButton")
WebElement paypalLoginBtn;
Please suggest any remedy for this problem............thanks in advance.

Tutorial or tips needed to perform a set of web admin tasks using Selenium(Beginner level query)

I'm looking at a few pointers /a tutorial or maybe even another question to help me with my task. I'm looking to automate a web admin task. What I would like to do is :
Login to an application.
Navigate to a particular menu.
Search for a particular item through a search bar.
If the item is displayed in the search items then click on a set of buttons on the UI and proceed with the task.
If the item is not displayed in the search results then continue searching till the item is displayed , and then perform step 4.
I have been able to perform up to step 3 using the selenium IDE plugin for Firefox. But I'm not quite sure how to proceed and where to incorporate the logic for steps 4 and 5. Do I use a programming language?(If yes, then how?)
You hit the limits of the IDE pretty quickly. The IDE doesn't incorporate logic, but it is good for quick and dirty automation tasks, figuring out locator id's, and helping people learn the basics of selenium. I would suggest checking out learning how to script in Selenium Webdriver. Documentation: http://seleniumhq.org/docs/03_webdriver.html
So for example if you're using Java (this was stolen from the documentation):
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");;
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
//Pseudocode
if(element.isDisplayed()){
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
}
else{
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
}
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}

How to do a forced page refresh using selenium webdriver?

In my automation, at one point I have to refresh the page to get the updated page content. But during the refresh the webpage is asking for a confirmation to resend the data (basically an alert is displayed on the page).
Even though I switched the focus to the alert and accepted it, the page contents are not getting refreshed. If I manually do the same, page contents are getting refreshed.
Is there any alternative way to refresh the page using Selenium Webdriver apart from navigate().refresh() command?
Or is there any way I can click on the Retry button on the alert without accepting the alert??
In ruby try using this - #driver.switch_to.alert.accept
this will click on Resend
java code driver.switchTo().alert().accept();
Refreshing Page in Selenium Webdriver using Java:
public boolean refresh()
{
boolean executedActionStatus = false;
try{
((JavascriptExecutor)driver).executeScript("document.location.reload()");
Thread.sleep(2000);
executedActionStatus = true;
}
catch(Exception er)
{
er.printStackTrace();
getScreenShot(method_TableName, "ExpCaseShot");
log.error(er);
}
return executedActionStatus;
}

How can I test context menu functionality in a web app?

I'm playing with a grails app that has a contextmenu (on right-click).
The context menu is built using Chris Domigan's jquery contextmenu plugin.
While the contextmenus do actually work, I want to have automated tests, and I can't work out how to do it.
I've tried Selenium 2.05a (ie. Webdriver), but there's no rightClick method.
I notice that HtmlUnit has a rightclick method, but I don't seem to be able to detect any difference in the DOM between before the click and after it.
Currently there's no right click method in WebDriver, there's an enhancement request opened for it - http://code.google.com/p/selenium/issues/detail?id=161
For now you can use keyboard shortcut Shift+F10 to simulate the right click on the element:
WebElement element = driver.findElement(....);
element.sendKeys(Keys.chord(Keys.SHIFT, Keys.F10));
While I'd like to be able to do it in Internet Explorer or Firefox as well, the main usage will be HtmlUnit. It's nice that the HtmlUnit HtmlElement has a rightClick() method, but unfortunately it's protected and so not accessible from the WebDriver wrapped HtmlUnitWebElement.
I wrote a hack to make it accessible, and so now I can call rightClick(), although it only works if it's running with HtmlUnit - not IE or FF.
// Needs to be in this package to get access to the element
package org.openqa.selenium.htmlunit;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
public class OpenHtmlUnitWebElement extends HtmlUnitWebElement {
// Provide a constructor, even though we don't really need it.
public OpenHtmlUnitWebElement(HtmlUnitDriver parent, HtmlElement element) {
super(parent, element);
}
// this is the method we really want.
public static HtmlElement using(HtmlUnitWebElement huwe) {
return huwe.element;
}
}
Now my (groovy) test looks like this:
import static org.openqa.selenium.htmlunit.OpenHtmlUnitWebElement.using
...
def itemWithContextMenu = driver.findElement(By.id('theId'))
if (itemWithContextMenu instanceOf HtmlUnitWebElement) {
using(itemWithContextMenu).rightClick()
def contextMenu = driver.findElement(By.id('jqContextMenu'))
assert ...
}
if you use Ruby with Capybara, this one should be useful:
module Capybara
module Node
class Element
def context_click
#session.driver.browser.action.context_click(self.native).perform
end
end
end
end