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

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;

Related

how to click dynamic links like ads on a webpage using selenium

I am new to Selenium webdriver, and trying to test a webpage which has some dynamic links (dynamic ads). Example : ads on https://mail.rediff.com/cgi-bin/login.cgi
I tried with xpath ,classname and id but none of these is working.It is happening because every time new content is displaying on the page so it is not able to find the element and throwing Exception in thread "main":
org.openqa.selenium.NoSuchElementException: Unable to locate element: #map.
my code is :
package Selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Image_Link {
static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//System.setProperty("webdriver.chrome.driver", "E://chromedriver_win32//chromedriver.exe");
//driver = new ChromeDriver();
System.setProperty("webdriver.gecko.driver", "E://geckodriver-v0.21.0-win64//geckodriver.exe");
driver = new FirefoxDriver();
Actions actions = new Actions(driver);
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
//WebElement Dynamic_ads=driver.findElement(By.className("rhs-area floatR"));
WebElement Dynamic_ads=driver.findElement(By.id("map"));
actions.moveToElement(Dynamic_ads).perform();
WebElement ad_Link = driver.findElement(By.cssSelector("#map > area:nth-child(2)"));
actions.moveToElement(ad_Link);
actions.click();
actions.perform();
//driver.navigate().to("www.google.com");
//String value = driver.findElement(By.id("hplogo")).getAttribute("title");
}
}
The elements for the dynamic ads cannot be found because it isn't fully loaded yet on load page. I recommend adding an explicit wait time for the specific add you're looking for. You can check out this link here for more information.
Anyhow, here's your solution:
1.)Implement WebDriverWait: WebDriverWait wait = new WebDriverWait(driver, 20);
2.) Change your Dynamic_ads data to wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("map"))); then do Dynamic_ads.click(); afterwards.
OR
2.) Change your Dynamic_ads data to wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//map[#id='map']/*"))); then do Dynamic_ads.click(); afterwards. NOTE: This will pick the first child node.

Unable to identify the webelements on XHTML page

Unable to identify the webelements on XHTML page
In My project if I click on a link a PDF opens up in a new tab and I am trying to download the PDF by clicking on Download click.The Web Element is not getting identified as the download icon is on the XHTML page. I have tried replicating this using India Post website. Still unable to identify the element. Below is the code. Does anyone have a solution?
package com.demo.learn;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class handle {
public static void main (String [] args){
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "D://CAHCO Automation//OpteamixAutomation-master(1)//OpteamixAutomation-master//Automation_framework//OPTEAMIX//Drivers//chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://www.indiapost.gov.in/VAS/Pages/Form.aspx");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245']/div[1]/table[1]/tbody/tr[2]/td[1]/a/img")).click();
Set<String> handles=driver.getWindowHandles();
String firsthandle = driver.getWindowHandle();
handles.remove(firsthandle);
String winhandle = handles.iterator().next();
if (winhandle!=firsthandle){
String secondWinHandle = winhandle;
driver.switchTo().window(secondWinHandle);
System.out.println("Switched");
boolean display = driver.findElement(By.xpath(".//*
[#id='download']")).isDisplayed();
if(display==true)
{
System.out.println("Displayed");
}
else
{
System.out.println("Not Displayed");
}
}
}
}
I guess what you are trying to do is clicking on PDF viewer in Chrome. The Chrome PDF viewer is a Chrome plugin so you cannot access it.
I recommend a workaround by adding an attribute to the link to open PDF. The attribute download will tell web browser to download it instead of open it in PDF Viewer. See my code below.
String script = "document.querySelector('#ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245>div>table>tbody>tr>td>a>img').setAttribute('download','name-of-the-download-file-recommend-guid-or-timestamp.pdf');";
((JavascriptExecutor)driver).executeScript(script);
driver.findElement(By.xpath(".//*[#id='ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245']/div[1]/table[1]/tbody/tr[2]/td[1]/a/img")).click();
Please note that the Css Selector in my example #ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245>div>table>tbody>tr>td>a>img is just an example which was converted from your XPath. It might find an incorrect element.

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

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')]

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

How do I get Selenium 2 webdriver to work with Nightly (Firefox 64 bit)

I'm looking to execute a Selenium 2 test against the Nightly browser (FireFox 64bit). It records just fine with the Selenium IDE (v1.8.1). And it also plays back just fine using using the IDE. I then export the code to TestNG format. By the way I've loaded up the Webdriver Backed plugin so it exports WebDriver code for the Selenium 2 version. The problem I'm having is that when I export the code to TestNG format (Java) and execute it, the asserts never find the text on the screen. It executes fine so its not that the code didn't convert. It just seems to be something with the asserts. If I play it from the IDE plugin it finds it the text and asserts just fine, however as soon as it executes in Java it fails all the assertions. Any ideas to what might be going on. My code is below. Thanks much!
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static junit.framework.Assert.*;
import com.thoughtworks.selenium.Selenium;
public class TestWithConfig {
WebDriver driver;
Selenium selenium;
#BeforeMethod
public void startSelenium() {
driver = new FirefoxDriver();
selenium = new WebDriverBackedSelenium(driver,
"http://en.wikipedia.org/wiki/Main_Page");
}
#AfterMethod
public void stopSelenium() {
driver.close();
}
#Test
public void testTest() {
selenium.setSpeed("600");
selenium.open("/wiki/Main_Page");
assertTrue("face not found",selenium.isTextPresent("face"));
selenium.click("link=Contents");
selenium.waitForPageToLoad("30000");
assertTrue("Below not found",selenium.isTextPresent("Below"));
selenium.click("link=Toolbox");
selenium.click("link=What links here");
selenium.waitForPageToLoad("30000");
assertTrue("Pages not found",selenium.isTextPresent("Pages that link to"));
selenium.click("link=exact:Talk:Wine");
selenium.waitForPageToLoad("30000");
assertTrue("Some not found",selenium.isTextPresent("Some"));
}
}
Since your using selenium 2 and webdriver, Assert's work a little different. I can see that you using the WebDriverBackedSelenium. However keep in mind. That's not selenium2. That is just a way to ease into selenium 2. I would use something like this.
WebElement tooltip = driver.findElement(By.xpath("the xpath of the element"));
assertNotNull("Name:","IP Address:",tooltip);
What I'm doing here is. i'm looking for a tooltip.inside that tooltip, there are two main labels that stay the same: Name and IP Address:. So I'm testing to see if those words exists or not in the tool tip. The output should be Name: IP Address:. That tells me the answer is true.

Resources