how to select the one suggestion randomly among the five suggestions from google search box - selenium-webdriver

By using this code i can click one suggestion from all suggestions. I cannot click the suggestions randomly. I need to click one suggestion randomly and verify only the clicked the suggestion is displayed or not.
package google;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Google {
public WebDriver driver;
#Test(priority=1)
public void Firefoxaccess() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:/Users/naveenkumar.d/Downloads/geckodriver-v0.17.0-win64/geckodriver.exe");
driver=new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.findElement(By.id("lst-ib")).sendKeys("n");
Thread.sleep(3000);
driver.findElement(By.xpath("/html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li[1]/div/div[2]")).click();
}
}

You could compose a selector based on a rand method.
For example:
r = generate random number here
and the selector should be something like:
//ul[#role='listbox']/li[r]
As for how to check that the option was selected you need to check that the search input has your text.
Text should be what you typed concatenated with the text from your random option, meaning the text from //ul[#role='listbox']/li[r]//b
And the selector for the check: //input[#name='q'][contains(#value, 'your_text')]

Related

How to test the 'drag and drop' feature from the react-beautiful-dnd library using Selenium in Java?

I have tried various options, but have been unable to simulate a mouse click to drag an element from one position to another in a browser using Selenium. When the test runs, I see the element get selected, but it does not move to the specified drop point. Any suggestions or insights are greatly appreciated!
Here's how I defined the function in my latest attempt (variations on this theme also tried and failed):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
builder.clickAndHold(dragPoint).perform();
builder.pause(Duration.ofSeconds(1));
builder.moveByOffset(10,0).perform();
builder.moveToElement(dropPoint).perform();
builder.moveByOffset(10,0).perform();
builder.pause(Duration.ofSeconds(1));
builder.release();
builder.build();
builder.perform();
}
Also tried the following (same result):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
Action dragAndDrop = builder.dragAndDrop(dragPoint, dropPoint).build();
dragAndDrop.perform();
}
In the test, the 2 elements are identified uniquely using xpath and the function is called:
WebElement dragPoint = driver.findElement(By.xpath(".../div[3]/...(etc.)/div[#class='rst__moveHandle']"));
WebElement dropPoint = driver.findElement(By.xpath(".../div[5]/...(etc.)/div[#class='rst__moveHandle']"));
dragAndDrop(dragPoint, dropPoint, driver);
Relevant libraries:
react-beautiful-dnd: https://github.com/atlassian/react-beautiful-dnd
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

Compilation error in Selenium Webdriver : field package.Classfile.driver is not visible

I am getting some sort of compilation error it seems.
I have ‘Common.java’ class inside ‘base’ package. It's a program for starting firefox driver. That, I have kept it in the separate package to make it a one time effort and modularity purpose.
I am calling this class file inside the child class ‘tc_01.java’. This TC_01.jave program is in another package ‘testing’. This TC_01.java file is actually accessing driver from Common.java and start the browser and try some login and logout actions.
My child class TC_01.java is showing me compilation error and Error Message on Mouse Hover is – > “field Common.driver is not visible”.
And, at Console : "java.lang.Error: Unresolved compilation problems:
The field Common.driver is not visible"
My Analysis: It seems TC_01.java file is not able to access the 'driver' from 'Common.java'.
But, I have already written 'extends' keyword for it to inherit the properties.
Please guide me. Thanks
Here is my code:->
package base;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class Common {
public FirefoxDriver driver;
#BeforeMethod
public void BM(){
System.setProperty(“webdriver.gecko.driver”,”D://Rajiv//Selenium//geckodriver.exe”);
driver = new FirefoxDriver();
driver.get(“http://automationpractice.com/index.php”);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#AfterMethod
public void CM(){
driver.close();
}
}
# Pakage – testing; Class – Tc_01.java
package testing;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import base.Common;
public class TC_01 extends Common{
public FirefoxDriver driver;`
#Test
public void TM(){
System.out.println(“Selenium Webdriver Script in Firefox browser using Gecko` `Driver | AutomationPractice.com PAGE LAUNCHED”);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“search_query_top”)));
try{
String expectedTitle = “My Store”;
System.out.println(“ExpectedTile = “+expectedTitle );
String actualTitle = driver.getTitle();
System.out.println(“The actual Title of the Page is = “+actualTitle);
Assert.assertEquals(actualTitle, expectedTitle);*/
System.out.println(“Control has reached here”);
driver.findElementByClassName(“login”).click(); // field common.driver is not visible
driver.findElementById(“email”).sendKeys(“*****#yahoo.com”);
driver.findElementById(“passwd”).sendKeys(“*****”);
driver.findElementById(“SubmitLogin”).click();
driver.findElementByClassName(“logout”).click();
System.out.println(“Sucessfully Logout from the Application”);
}catch (Exception e){
e.printStackTrace();
}
}
}
I was able to find the root cause for this issue.
It wasted my 3 days before i asked for help here in this forum.
As i had analysed initially, i had mentioned my driver properties in parent class and was accessing it in Child Class. There was some name mismatch. When it was trying to inherit the properties from parent, it was not accessible and giving me compilation error. I renamed the parent class name to the same name that i was giving while extending in Child Class. And, it worked.
Thanks again to all of you. It is such a wonderful forum to discuss your issue between each other and getting its resolution.

verify search working proprly using Selenium

I am beginner for Selenium, just I want to check that search of the website working properly. Means when I enter any keyword in the search box and click on search is it provides correct search result. How to Check using Selenium WebDriver. Please guide me.
You should provide more details about your query
for example you should include the URL your are working on or a similar URL for the one you are working on
You can use an item property from the search result page as an assertion point for your test
Try below code
package StackOverFlow.StackOverFlow;
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;
import org.testng.Assert;
import org.testng.annotations.Test;
public class App
{
#Test
public void test () throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\Chrome\\2\\chromedriver.exe");
WebDriver Dr = new ChromeDriver();
Dr.manage().window().maximize();
Dr.get("http://www.conns.com");
Thread.sleep(4000);
WebElement ele = Dr.findElement(By.xpath(".//*[#id='search']"));
ele.sendKeys("furniture");
ele.sendKeys(Keys.ENTER);
Thread.sleep(4000);
String result= Dr.findElement(By.xpath("/html/body/div[3]/div/div[4]/div[2]/div/div[1]/h1")).getText();
Assert.assertEquals("Furniture & Mattresses", result);
}
}
I used a very obvious element (Furniture & Mattresses) on the page to assert against its text

Selenium sample program: Getting Error org.openqa.selenium.WebDriverException: expected expression, got end of script

I am trying to do a sample program with selenium webdriver. I am using libraries from Selenium-java-2.53.1.
Here is my sample program
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
public class ScrollWebPage {
WebDriver driver;
String URL="https://www.gmail.com";
#BeforeClass
public void setUp(){
driver = new FirefoxDriver();
driver.get(URL);
driver.manage().window().maximize();
}
#Test(priority=1)
public void scrollingToBottom(){
((JavascriptExecutor) driver).executeScript(URL, "window.scrollTo(0,document.body.scrollHeight)");
}
#AfterClass
public void tearDown(){
driver.quit();
}
}
The page is getting opened but it is not able to scroll down. seems an issue with executeScript()
Please help
.executeScript() expecting JavaScript string expression as first arguments while you are providing simply a String as Url which is not an JavaScript expression as exception says, You need to change :-
((JavascriptExecutor) driver).executeScript(URL, "window.scrollTo(0,document.body.scrollHeight)");
to
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
Note :- .executeScript() expect arguments like String arg0, Object... arg1 which means first arguments should be String but it should be JavaScript expression and second arguments should be Array of Object like Object[]
In your case no need to provide URL as arguments if you simply want to execute scrolling function.
Hope it will help you..:)
Simply Use as below to see the scroll working. Try in some other page because gmail don't have a much bigger page to feel the scroll.
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,2500)");

unable to locate a element thru Selenium webdriver

I am trying to write a code to find cheapest tickets between two locations .I am stuck in the beginning of the code because selenium does not recognise simple input text box . here is the code .
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class orbitzsimpleTest {
public static void main(String args[]) throws InterruptedException {
WebDriver dr = new FirefoxDriver();
dr.navigate().to("https://www.orbitz.com/");
Thread.sleep(5000);
System.out.println("after threat sleep");
dr.findElement(By.xpath(".//*[#id='flight-origin']")).sendKeys("SFO");
}
}
I also tried to write the XPath using contains like
List<WebElement> s = dr.findElements(By.xpath(".//*[contains(#id,'flight_*')]"));
No luck. It looks so simple but i'm struggling to go past this.
You can just get this element with dr.findElement(By.id('flight-origin')) or (By.cssSelector('#flight-origin')).
And it's better to use 'smart' waits (wait for element to be displayed/clickable/visible) instead of sleep. Because it can be that 5 secs is not enough and you try to send key to the input which isn't loaded yet.
I think i found the reason why it was not recognising that element. It's because the flight tab had to be selected before it finds this text box .I was able to fix it .
Thank you so much for your input .

Resources