Not able to perform drag & drop using Actions - java

package Chrome_Packg;
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 testFirefox_DragDrop {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
WebElement drag=driver.findElement(By.xpath("/html/body/div[1]"));//drag element
WebElement drop=driver.findElement(By.xpath("/html/body/div[2]"));//drop element
Actions action=new Actions(driver);
Thread.sleep(3000);
action.dragAndDrop(drag, drop).perform();
}
}
After executing the code, using Run as java application, in output I am getting nothing.

Here is the code snippet for the same website that you are trying on and you can also find the example here selenium Drag and Drop example
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://jqueryui.com/droppable/");
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".ui-droppable"));
dragAndDrop(Sourcelocator,Destinationlocator);
String actualText=driver.findElement(By.cssSelector("#droppable>p")).getText();
Assert.assertEquals(actualText, "Dropped!");

Related

Facebook post using java selenium

I am trying to post some text and pictures on facebook with a little Java program.
I am not able to post anything since I cannot click the appropriate button with selenium.
Here is the part of code where I need to click the upload Photo/video button on:
package good;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.interactions.Actions;
public class HareKrishna {
public static void main (String []args ) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver","/home/gt8201/AjaySingh/Automation Tools /chromedriver_linux64/chromedriver_linux64(1)/chromedriver");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.facebook.com/");
driver.findElement(By.name("email")).sendKeys("abc");
driver.navigate().forward();
driver.findElement(By.id("pass")).sendKeys("abx");
driver.findElement(By.name("login")).click();
driver.manage().window().maximize();
Thread.sleep(5000);
Alert alert = driver.switchTo().alert();
driver.switchTo().alert().accept();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.navigate().forward();
Thread.sleep(5000);
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
Actions Act = new Actions(driver);
driver.findElement(By.xpath("//*[#id=\"mount_0_0_08\"]/div[1]/div[1]/div/div[3]/div/div/div[1]/div[1]/div/div[2]/div/div/div/div[3]/div/div[2]/div/div/div/div[1]/div/div[1]/span"));
Act.click().sendKeys("Hare krishna");
}
}
The first step is click on this xpath //*[#class="sjgh65i0"]//descendant::div[#class="m9osqain a5q79mjw gy2v8mqq jm1wdb64 k4urcfbm qv66sw1b"] and it do to appear the pop-up "create publication".
Now you could add a comment with xpath //*[#method="POST"]//descendant::*[#role="presentation"]//descendant::*[#role="textbox"] or push button "add videos/photos" //*[#method="POST"]//descendant::div[#class="rq0escxv l9j0dhe7 du4w35lb j83agx80 cbu4d94t buofh1pr tgvbjcpo"]//descendant::*[#role="button"]
Later you could push button publish //*[#method="POST"]//descendant::div[#class="k4urcfbm discj3wi dati1w0a hv4rvrfc i1fnvgqd j83agx80 rq0escxv bp9cbjyn"]//descendant::*[#role="button"]

How can I open subtabs of a tab for testing in selenium

I am trying to open subtabs of blog tabs. I try to test clickablity of all topics in blog page
This is page's title codes
http://prntscr.com/r8janx
here is my codes
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BlogTestEN {
//143
// /html/body/section/div[2]/div[1]/div/div[1]/ul/li/a
public static void main(String[] args) {
System.setProperty("webdriver.firefox.driver",
"C:\\\\Users\\\\Abdullah Kandilli\\\\eclipse-workspace\\\\Selenium.maven.demo\\\\chromedriver.exe");
//Initiating your chromedriver
WebDriver driver=new ChromeDriver();
driver.get("https://www.rentnconnect.com/blog");
//driver.findElement(By.xpath("/html/body/header/div/div[3]/div/div/div[2]/div/form/span[2]/div[1]/div/div/div[1]/input")).sendKeys("Turkey",Keys.ENTER);
//driver.executeScript("window.history.go(-1)");
for(int i = 1; i<144; i+= 2) {
driver.manage().timeouts().implicitlyWait(1500, TimeUnit.SECONDS);
driver.findElement(By.xpath("/html/body/section/div[2]/div[1]/div/div["+i+"]")).click();
driver.manage().timeouts().implicitlyWait(1500, TimeUnit.SECONDS);
driver.navigate().back();
driver.manage().timeouts().implicitlyWait(1500, TimeUnit.SECONDS);
}
}
}
But it opens blog page and click by xpath(correctly) but tests only 1 blog tabs. I want to test all ingredients and xpaths changing by /html/body/section/div[2]/div[1]/div/div["+i+"] div["+i+"] section with for loop.

How to close two tabs and open a new tab with different url

I have a test case in QA where guest clicks on a link and a new tab will opens. But this new tab is defaulting to Prod, so I don't want to continue my test case in prod and I want this tab to close and navigate to a different url (the one that pointing to QA). How I can implement this using selenium web driver?
Instead of clicking the link you can save it's href attribute and let the driver load url you want. Or you can switch between tabs. Following code should help you.
package links;
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Links {
public static void main(String[] args) {
// init driver
String chromeDriverPath = "C:\\Users\\pavel.burgr\\Desktop\\webdrivers\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
// get EN page
driver.get("https://en.wikipedia.org/wiki/Hyperlink");
waitSec(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("p-logo")));
WebElement hyperlinked = driver.findElement(By.xpath("//a[contains(#href,'/wiki/Hyperlinked')]"));
// get the href attribute of webelement 'Hyperlinked'
String href = hyperlinked.getAttribute("href");
// change the href
String alteredHref = href.replace(".en", ".cs");
// open new Tab
openAndGoToNewTab(driver);
// load page with altered url (czech version of the same page)
driver.get(alteredHref);
waitSec(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("p-logo")));
}
// custom wait method
public static WebDriverWait waitSec(WebDriver driver, int sec) {
return new WebDriverWait(driver, sec);
}
// opens new tab and switch to it
public static void openAndGoToNewTab(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size() - 1));
}
// close current tab
public static void closeTab(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.close()");
}
// switch to tab by index
public static void swotchToTab(WebDriver driver, int tabIndex) {
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabIndex));
}
}

How to write explicit wait until a particular WebElement is located

This is a sample code for the explicit wait:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath-here")));
I want to pass WebElement as a parameter in method and wait until that WebElement is located:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(MyWebElement));
I am not sure whether such option is already there and can be done in a different way as in my case I am getting an exception as I am passing WebElement in place of By.xpath("") which is not the correct way.
You need to use the visibilityOf expected condition.
The code will be like:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(MyWebElement));
Hope it helps you!
Yes, you cannot pass the web element in place of XPath because the method 'visibilityOfElementLocated()' will accept only By locator.
Suppose, your method is like below :
public static void waitUntilLocated(WebDriver driver, int waitingTime, By locator) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOfElementLocated(locator));
}
Then You can store the By locator like below :
By someXPath = By.xpath("some xpath here");
Then you can pass 'By locator' to the method as a parameter
waitUntilLocated(driver, 30, someXPath);
Below is the whole code :
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test1 {
public static void waitUntilLocated(WebDriver driver, int waitingTime, By locator) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOfElementLocated(locator));
}
public static void main(String ...ali) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:1234/wd/hub"), capabilities);
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Alicse3"+Keys.ENTER);
// Checking element is located or not?
By someXPath = By.xpath("some xpath here");
waitUntilLocated(driver, 30, someXPath);
// Checking some other element
By someId = By.id("some id");
waitUntilLocated(driver, 30, someId);
}
}
Or you can do like below if you want to pass Web Element to the method and don't want to use 'visibilityOfElementLocated()' :
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test1 {
public static void waitUntilLocated(WebDriver driver, int waitingTime, WebElement element) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOf(element));
}
public static void main(String ...ali) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:1234/wd/hub"), capabilities);
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Alicse3"+Keys.ENTER);
// Checking element is located or not?
WebElement element = driver.findElement(By.xpath("Some XPath"));
waitUntilLocated(driver, 30, element);
// Checking some other element
element = driver.findElement(By.id("Some ID"));
waitUntilLocated(driver, 30, element);
}
}
I hope it helps...

Cant select elements on Webpage

I'm having major issue trying to select menu two elements on a dropdown.I've tried xpaths, link texts and css selector but it wont select either the password button or logout button.
Xpaths used for Password button: "//*[#id='app']/header/div[3]/nav/ul/li/a"
CSS used for Logout button: ["data-logged-in-log-out-button"]
XPath used for Logout button: "//*[#id='app']/header/div[3]/nav/ul/a"
The error im getting for the select password is:
org.openqa.selenium.WebDriverException: unknown error: Element ... is not clickable at point (989, 233).
Other element would receive the click: ...
Please try with the below XPath along with the explicit wait condition.
XPath:
//*[contains(text(),'Log out')]
Can you please try -
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class A {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("url here");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log Out")));
driver.findElement(By.linkText("Log Out")).click();;
}
}
If it still doesn't work, please try -
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class A {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("url here");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", driver.findElement(By.linkText("Log Out")));
}
}

Categories

Resources