Selenium Test doesn't run - selenium

I'm writing a program for Booth's Multiplication as a JAVA Web Application in Netbeans 8.2. I am using selenium-server-standalone-2.50.1.jar and Firefox 46.
When I write my code and run it, it just opens the page but no tests happen. Please help.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*
*/
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class booth {
private WebElement element, element1;
public String str;
public void Mul_Positive(){
WebDriver driver = new FirefoxDriver();
driver.get("http://localhost:8080/BoothsMul/index.html");
element = driver.findElement(By.xpath("//input[#name='t1']"));
element.sendKeys("2");
str = driver.findElement(By.xpath("//input[#name='t1']")).getText();
System.out.println(str);
element1 = driver.findElement(By.xpath("//input[#name='t2']"));
element1.sendKeys("5");
str = driver.findElement(By.xpath("//input[#name='t2']")).getText();
System.out.println(str);
driver.findElement(By.xpath("//input[#name='submit']")).click();
driver.quit();
}
public void Mul_Negative(){
WebDriver driver = new FirefoxDriver();
driver.get("http://localhost:8080/BoothsMul/index.html");
element = driver.findElement(By.xpath("//input[#name='t1']"));
element.sendKeys("c");
str = driver.findElement(By.xpath("//input[#name='t1']")).getText();
System.out.println(str);
element = driver.findElement(By.xpath("//input[#name='t2']"));
element.sendKeys("5");
str = driver.findElement(By.xpath("//input[#name='t2']")).getText();
System.out.println(str);
driver.findElement(By.xpath("//input[#name='submit']")).click();
driver.quit();
}
public static void main(String[] args){
booth b = new booth();
b.Mul_Positive();
b.Mul_Negative();
}
}
My index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<body>
<form name='f1' action='http://localhost:8080/BoothsMul/Boothmul' method='get'>
First No. <input type='text' name='t1'><br>
Second No. <input type='text' name='t2'><br>
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>

UPDATED:
If you're using selenium 3.0.1 or higher then JAVA version should be 1.8 also, you'll need to make use of geckodriver to run Firefox. (Make sure you are using latest versions of all the required component in this case)
Else, you need to upgrade your Selenium Version to 3.0 or higher.

Related

Verification of hover over functionality using Selenium

I need to verify if hover over is working for a webelement using selenium. I know there are lots of answers asking to use either action class or getTitle().For ex: https://www.guru99.com/verify-tooltip-selenium-webdriver.html
In both these solutions, it is about getting the text and asserting it. But my question is how can it ensure that hover over is working (I mean, when the user does a hover over the tooltip text should be displayed). For ex: in the below code, Actions class is used to clickAndHold and moveToElement. And then getText() is done to get the hover over text. Isn't the end result the same as using WebElement.getText() without using Actions class?
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.*;
public class JqueryToolTip {
public static void main(String[] args) {
String baseUrl = "http://demo.guru99.com/test/tooltip.html";
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String expectedTooltip = "What's new in 3.2";
driver.get(baseUrl);
WebElement download = driver.findElement(By.xpath(".//*[#id='download_now']"));
Actions builder = new Actions (driver);
builder.clickAndHold().moveToElement(download);
builder.moveToElement(download).build().perform();
WebElement toolTipElement = driver.findElement(By.xpath(".//*[#class='box']/div/a"));
String actualTooltip = toolTipElement.getText();
System.out.println("Actual Title of Tool Tip "+actualTooltip);
if(actualTooltip.equals(expectedTooltip)) {
System.out.println("Test Case Passed");
}
driver.close();
}
}
getText()
getText() gets the visible (i.e. not hidden by CSS) text of this element, including sub-elements.
Without Mouse Hover Download now button's following-sibling <div> element will be having style attribute set as display: none;
In those cases, Selenium won't be having visibility on the element
<div class="tooltip"... display: none;>
and you may face NoSuchElementException.
Where as, if you Mouse Hover the Download now button's following-sibling <div> element will be having style attribute set as display: block;
Then Selenium would be having visibility on the element
<div class="tooltip"... display: block;>
and you can extract the required texts.

How to simplify the following code using Selenium Webdriver?

<div class="col autocomplete theme-standard pin-left" data-control="autocomplete" data-auto-pos="true">
<label class="text autocomplete-arrow icon-before" id="FH-origin-label">
<span class="label">Leaving from</span>
<span class="visuallyhidden">City or airport</span>
<input type="text" name="FrAirport" data-canonic="origin" id="FH-origin" data-minchar="3" data-provide="autocomplete" data-template="#uitk-autocomplete-default" data-theme="autocomplete" data-closetext="Close" data-continuetext="Continue typing to refine search" data-lob="PACKAGES" data-mask="95" data-version="v4" data-locale="en_US" data-forceicon="flights" data-autoselect="touch" data-selectioncallback="publishingWizardPackageTypeAheadOriginCallback" placeholder="City or airport" xpath="1">
<span class="icon icon-location" aria-hidden="true"></span>
</label>
<div class="autocomplete-dropdown"></div>
</div>
I am trying to select one of the 'Leaving from' section on the following website using chrome webdriver using while loop: https://alaskatrips.poweredbygps.com/g/pt/hotels?MDPCID=ALASKA-US.TPS.BRAND.hotels.HOTEL
I have tried the code below:
package dropdowns;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class synchronization {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\everybody\\Desktop\\selenium\\library\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://alaskatrips.poweredbygps.com/g/pt/hotels?MDPCID=ALASKA-US.TPS.BRAND.hotels.HOTEL");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement a = driver.findElement(By.id("FH-origin"));
a.sendKeys("NEW");
while (!a.equals("New Haven, CT (HVN-All Airports)")) {
//Thread.sleep(4000);
driver.findElement(By.id("FH-origin")).sendKeys(Keys.ARROW_DOWN);
driver.findElement(By.id("FH-origin")).sendKeys(Keys.ENTER);
}
Is this what you're looking for?
while (!a.getText().equals("New Haven, CT (HVN-All Airports)")) {
Thread.sleep(4000); //what is the need of this Thread sleep?
driver.findElement(By.id("FH-origin")).sendKeys(Keys.ARROW_DOWN);
}
driver.findElement(By.id("FH-origin")).sendKeys(Keys.ENTER);
i just fixed your code based on my assumptions
posting the HTML would help other SO users find the answer for your question
Code simplification:
In the given page,
after entering a few letters it loads the autocomplete suggestions in a list.
You have to wait for the autocomplete drop down to show up. To do that use explicit wait because it waits until the element is visible and not more.
Explicit Wait:
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(autocomplete));
After that, you have to find the item that contains 'New Heaven' and click on it. You can use XPath to find elements containing specific text.
XPath:
//*[#id='backwards']/li/a[contains(.,'New Haven')]
Now that you already found the item, you don't have to run a loop to find the element anymore.
TRY This:
driver.get("https://alaskatrips.poweredbygps.com/g/pt/hotels?MDPCID=ALASKA-US.TPS.BRAND.hotels.HOTEL");
WebElement leavingFrom = driver.findElement(By.id("FH-origin"));
WebElement autocomplete = driver.findElement(By.className("autocomplete-dropdown"));
leavingFrom.sendKeys("NEW");
//after entering sample text wait for the autocomplete drop-down to show up
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(autocomplete));
WebElement newHaevn = driver.findElement(By.xpath("//*[#id='backwards']/li/a[contains(.,'New Haven')]"));
newHaevn.click();
For this, you have to import
import org.openqa.selenium.support.ui.WebDriverWait;
Possible Error Fix:
For the Chrome not reachable exception possible solutions are:
Update the selenium
Update the Chrome to latest version
Update the chromedriver for the specific chrome version
Use the no-sandbox chrome option.
ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox");
chromeDriver = new ChromeDriver(options);
-

how handle auto suggest in "from" and "destination" box for this website "https://www.goibibo.com/" in selenium

how handle auto suggest in "from" and "destination" box for this website "https://www.goibibo.com/" in selenium.
please help
I tired using the basic method but unable to get the X path of the auto suggestion drop down
Unable to click on the drop down
package basic;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class goibibo {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new ChromeDriver();
driver.get("https://www.goibibo.com/");
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc']")))
.sendKeys("Mum");
List<WebElement> myList = new WebDriverWait(driver, 20).until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[#id=\"react-autosuggest-1\"]")));
for (WebElement element : myList) {
if (element.getText().contains("Mumbai"))
;
element.click();
}
}
}
Chrome Browser
First how to Find XPATH of auto populate box in Chrome Browser open your website than click on Inspect element and click on source Tab now, click for opening your auto populate box and Press **F8** **Key for pause debugger**. Then click on your Element tab and you can easily get your xpath refer below snap for more information. so it will freeze your HTML.
Now click on Elements an Create your own xpath.
Fire Fox Browser
Second how to find xpath of Auto Populate box in Firefox - Open your Firefox and Right click and click on inspect elements on your website. there is option of animation so it will open all your DOM Expanded like below image. so by reading this dom structure you can create easily your XPATH.
Not how to find Elements from auto populate box. Refer below code snippet for that.
package com.software.testing;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Testingclass extends DriverFactory {
private static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "your driver path");
driver = new ChromeDriver();
driver.get("https://www.goibibo.com/");
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc']")))
.sendKeys("A");
Thread.sleep(1000);
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(
By.xpath("//div[#class='dib marginL10 pad0 textOverflow width90']/div/span")));
for (int i = 0; i < myList.size(); i++) {
System.out.println(myList.get(i).getText());
if (myList.get(i).getText().equals("Ahmedabad")) {
myList.get(i).click();
break;
}
}
}
}
Don't forgot to use break after your conditional statement else it
will thrown an exception.
So you can try one solution please find the below screenshot,
As you can see in screenshot if i type M in text box then dropdown shows the record respect to letter 'M' and if you see in source the <ul> which is dynamic as you see just below <input> so you need to handle that dropdown by it's locator it is dynamic hence first you need to pass some text in text box and after that you need to select the element from the drop down using Select in selenium you use selectByVisibleText("") or what ever or you can use List<Element> you can store all the respected sources (Mumbai, Mysore ,etc)coming from dropdown and use it wisely
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc'"]))).sendKeys("M");
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("Xpath of the dynamic drop down")));
for (WebElement element:myList) {
if(element.getText().contains("Mumbai"));
element.click();
}
I gave you an idea let me know if you need any further help
I have automated it through selenium with python. Its collecting all suggested cities in a list and then clicking the required one.
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.goibibo.com/")
driver.implicitly_wait(3)
listCity = []
driver.find_element_by_xpath("//input[#id='gosuggest_inputSrc']").send_keys("JA")
cities = driver.find_elements_by_xpath("//div[#class='mainTxt clearfix']//preceding-sibling::span")
for city in cities:
listCity.append(city.text)
for city in cities:
if "Jagdalpur" in city.text:
city.click()
break
print(listCity)
print(len(listCity))
Use below code it will work
Webelement ele=driver.findelement()
Actions ob = new Actions(driver);
ob.moveToElement(ele);
ob.click(ele);
Action action = ob.build();
action.perform();

How to handle calendar for "departure" box and "return" box in Selenium? [duplicate]

how handle auto suggest in "from" and "destination" box for this website "https://www.goibibo.com/" in selenium.
please help
I tired using the basic method but unable to get the X path of the auto suggestion drop down
Unable to click on the drop down
package basic;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class goibibo {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new ChromeDriver();
driver.get("https://www.goibibo.com/");
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc']")))
.sendKeys("Mum");
List<WebElement> myList = new WebDriverWait(driver, 20).until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[#id=\"react-autosuggest-1\"]")));
for (WebElement element : myList) {
if (element.getText().contains("Mumbai"))
;
element.click();
}
}
}
Chrome Browser
First how to Find XPATH of auto populate box in Chrome Browser open your website than click on Inspect element and click on source Tab now, click for opening your auto populate box and Press **F8** **Key for pause debugger**. Then click on your Element tab and you can easily get your xpath refer below snap for more information. so it will freeze your HTML.
Now click on Elements an Create your own xpath.
Fire Fox Browser
Second how to find xpath of Auto Populate box in Firefox - Open your Firefox and Right click and click on inspect elements on your website. there is option of animation so it will open all your DOM Expanded like below image. so by reading this dom structure you can create easily your XPATH.
Not how to find Elements from auto populate box. Refer below code snippet for that.
package com.software.testing;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Testingclass extends DriverFactory {
private static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "your driver path");
driver = new ChromeDriver();
driver.get("https://www.goibibo.com/");
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc']")))
.sendKeys("A");
Thread.sleep(1000);
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(
By.xpath("//div[#class='dib marginL10 pad0 textOverflow width90']/div/span")));
for (int i = 0; i < myList.size(); i++) {
System.out.println(myList.get(i).getText());
if (myList.get(i).getText().equals("Ahmedabad")) {
myList.get(i).click();
break;
}
}
}
}
Don't forgot to use break after your conditional statement else it
will thrown an exception.
So you can try one solution please find the below screenshot,
As you can see in screenshot if i type M in text box then dropdown shows the record respect to letter 'M' and if you see in source the <ul> which is dynamic as you see just below <input> so you need to handle that dropdown by it's locator it is dynamic hence first you need to pass some text in text box and after that you need to select the element from the drop down using Select in selenium you use selectByVisibleText("") or what ever or you can use List<Element> you can store all the respected sources (Mumbai, Mysore ,etc)coming from dropdown and use it wisely
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc'"]))).sendKeys("M");
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("Xpath of the dynamic drop down")));
for (WebElement element:myList) {
if(element.getText().contains("Mumbai"));
element.click();
}
I gave you an idea let me know if you need any further help
I have automated it through selenium with python. Its collecting all suggested cities in a list and then clicking the required one.
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.goibibo.com/")
driver.implicitly_wait(3)
listCity = []
driver.find_element_by_xpath("//input[#id='gosuggest_inputSrc']").send_keys("JA")
cities = driver.find_elements_by_xpath("//div[#class='mainTxt clearfix']//preceding-sibling::span")
for city in cities:
listCity.append(city.text)
for city in cities:
if "Jagdalpur" in city.text:
city.click()
break
print(listCity)
print(len(listCity))
Use below code it will work
Webelement ele=driver.findelement()
Actions ob = new Actions(driver);
ob.moveToElement(ele);
ob.click(ele);
Action action = ob.build();
action.perform();

how do i click drop down menu using Selenium?

i'm trying to upload some sonar ruleset files to multiple sonars.
i want to get help by web ui automator using Selenium.
i wrote some java code but it still doesn't work.
*added comment
bellowed code works on chrome driver but it doesn't work on safari driver.
please tell me how to modify code to work for multiple browser.
here is my code
public void openQualityProfiles() {
String linkTextSettings = "Settings";
String cssSelector = ".dropdown-menu > ul > li > a";
WebElement settings = waitForElement(By.linkText(linkTextSettings));
settings.click();
WebElement qualityProfiles = waitForElement(By.cssSelector(cssSelector));
qualityProfiles.click();
}
public WebElement waitForElement(By locator) {
WebElement target = null;
WebDriverWait wait = new WebDriverWait(driver, 10);
target = wait.until(ExpectedConditions.elementToBeClickable(locator));
return target;
}
and here is HTML
Settings
<div id="admin-panel" class="dropdown-menu" style="display: none">
<ul>
<li>Quality Profiles</li>
<li>Configuration</li>
<li>Security</li>
<li>System</li>
If your submenu appears when mouse is over, you can use:
new Actions(driver).moveToElement(yourMenu).build().perform();
or try to click on
driver.findElement(By.className("link-more")).click();