I am having persistent problem on getting the drag and drop functionality working with Selenium WebDriver.
According the WebDriver documentation the drag/drop should work out of the box with command:
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
However this seems not to be working either with Firefox or Chrome driver.
Below is an example test that tries to use drag and drop functionality on 2 public draggable web sites. Test is parameterized and executed with both FirefoxDriver and ChromeDriver.
package test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
#RunWith(Parameterized.class)
public class DragAndDropTest {
enum Browser {FIREFOX, CHROME};
private Browser browser;
private WebDriver driver;
#Parameters
public static Collection<Object[]> data() throws Exception {
List<Object[]> params = new ArrayList<Object[]>();
params.add(new Object[] { Browser.FIREFOX });
params.add(new Object[] { Browser.CHROME });
return params;
}
public DragAndDropTest(Browser browser) {
this.browser = browser;
}
#Before
public void before() {
switch (browser) {
case FIREFOX:
this.driver = new FirefoxDriver();
break;
case CHROME:
this.driver = new ChromeDriver();
}
}
#After
public void tearDown() {
driver.quit();
}
#Test
public void test1() {
By drag = By.id("div1");
By drop = By.id("div2");
By expected = By.cssSelector("#div2 #drag1");
// load page
driver.get("http://www.w3schools.com/html/html5_draganddrop.asp");
// wait for draggable element visible
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(drag));
// drag and drop
new Actions(driver).dragAndDrop(driver.findElement(drag), driver.findElement(drop)).perform();
// verify results
Assert.assertEquals("Drag&Drop failed", 1,driver.findElements(expected).size());
}
#Test
public void test2() {
By drag = By.id("Item1");
By drop = By.id("Item5");
By expected = By.cssSelector("#DragContainer5 #Item1");
// load page
driver.get("http://www.webreference.com/programming/javascript/mk/column2/index.html");
// wait for draggable element visible
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(drag));
// drag and drop
new Actions(driver).dragAndDrop(driver.findElement(drag), driver.findElement(drop)).perform();
// verify results
Assert.assertEquals("Drag&Drop failed", 1, driver.findElements(expected).size());
}
}
Any suggestions/pointers on why the above tests are not working correctly?
W3C site has HTML5 drag and drop which is currently not supported by Webdriver. Refer to this issue - https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/3604
But in your first test case you should have used this for the drag element.
By drag = By.id("drag1");
This is the locator of the image you are dragging.
Related
I am trying to create a script in selenium using Java. But as soon as I run it, first, the desired page opens up but then automatically it is re-directed to that website's main page.
Here is the code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class second {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/Users/chromedriver");
WebDriver driver=new ChromeDriver();
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
String sampleText = driver.findElement(By.className("col-md-12")).getText();
System.out.println(sampleText);
driver.findElement(By.linkText("This is a link")).click();
driver.findElement(By.id("fname")).sendKeys("JavaTpoint");
driver.findElement(By.id("fname")).clear();
driver.findElement(By.id("idOfButton")).click();
driver.findElement(By.id("male")).click();
driver.findElement(By.cssSelector("input.Automation")).click();
Select dropdown = new Select(driver.findElement(By.id("testingDropdown")));
dropdown.selectByVisibleText("Automation Testing");
driver.close();
}
}
I am trying to do a POC for a windows app using winappdriver . I have the winappdriver version 1.1 installed up and running. I want to find the elements by using their automationId. As per winappdriver documentation, elements with AutiomationID can be located by "findElementByAccessibilityId". I am not able to see this locator strategy in my Eclipse intellisense. Instead "findElementsByAccessibilityId" ( notice elements) is been shown up. What should i do so that i can see "findElementByAccessibilityId" locator in intellisense.
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.windows.WindowsDriver;
public class LoginTest {
private static WindowsDriver<WebElement> driver = null;
#BeforeClass
public static void setup() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("app", "XXXXXXXXXXXXXXXXXXXXXX");
capabilities.setCapability("platformName", "windows");
capabilities.setCapability("deviceName", "windowsPC");
capabilities.setCapability("appWorkingDir", "XXXXXXXXXXXXXXXXXXXXXXXXXX");
driver = new WindowsDriver<WebElement>(new URL("http://127.0.0.1:4723"), capabilities);
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
}
#Test
public void Testing()
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.className("TextBox")).sendKeys("XXXX");
driver.findElementById("TxtPwd").sendKeys("XXXX");
driver.findElementsByAccessibilityId("TxtPwd");
driver.findElement(By.id("BtnLogin")).click();
System.out.println("Hi");
}
}
POM.xml
I am working on a Selenium testing framework tutorial of John Sonmez and ran into problem trying to perform the very first test. The test part consists of two methods: 1. LoginPage.GoTo(); which opens wordpress login page and 2. LoginPage.LoginAs("admin").Login(); which inserts username and clicks continue.
What happens when I run the test is wordpress loginpage opens and after 2 seconds blank Chrome browser opens and JUnit displays NoSuchElementException. The author of tutorial solved this problem adding some WebDriverWait and switchTo.ActiveElement.getAttribute() to LoginPage.GoTo(); method. However he is coding in C# and offers no solution for Java I'm coding in. I tried to apply WebDriverWait also but the problem is still there. Can anyone please suggest me how to solve this problem using WebDriverWait in Java.
LoginTest
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
import wordpressAutomation.Driver;
import wordpressAutomation.LoginPage;
public class LoginTest extends Driver {
#Test
public void admin_user_can_login() {
LoginPage l = new LoginPage();
l.GoTo();
LoginPage.LoginAs("scenicrail").Login();
//Assert.assertTrue(DashboardPage.IsAt, "Failed to login");
}
}
LoginPage
import org.junit.Test;
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 LoginPage extends Driver {
#Test
public void GoTo() {
openBrowser().get("https://wordpress.com/log-in");
}
public static LoginCommand LoginAs(String username) {
return new LoginCommand(username);
}
}
public class LoginCommand extends Driver {
private String username;
private String password;
public LoginCommand(String username) {
this.username = username;
}
public LoginCommand WithPassword(String password) {
this.password = password;
return this;
}
public void Login() {
WebElement login = openBrowser().findElement(By.xpath("//*[#id='usernameOrEmail']"));
login.sendKeys(username);
openBrowser().findElement(By.xpath("//button[#type = 'submit']")).click();
}
}
public class Driver {
public WebDriver openBrowser() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\KatarinaOleg\\Desktop\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
After opening the page, where you get the NoSuchElementException you can add in something like this.
FluentWait<WebDriver> webDriverWait = new WebDriverWait(driver, 25).pollingEvery(5, TimeUnit.SECONDS);
webDriverWait.until(ExpectedConditions.visibilityOf(webElement));
Just add in whatever element you want to use as your check to make sure the page has loaded
you have called the Openbrowser multiple times in Login Method from LoginCommand Class.So, It will be unnecessarily create different driver instance. So, your code needs to be modified as below along with explicit wait (Driver Class also needs to be changed as below)
Login Method from LoginCommand Class:
public void Login() {
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='usernameOrEmail']")));
WebElement login = driver.findElement(By.xpath("//*[#id='usernameOrEmail']"));
login.sendKeys(username);
driver.findElement(By.xpath("//button[#type = 'submit']")).click();
}
Driver Class:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Driver {
WebDriver driver;
public WebDriver openBrowser() {
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
Edit: (dmin_user_can_login() method in LoginTest class)
#Test
public void admin_user_can_login() {
LoginPage l = new LoginPage();
l.GoTo();
l.LoginAs("scenicrail").Login();
//Assert.assertTrue(DashboardPage.IsAt, "Failed to login");
}
LoginPage Class:
import org.junit.Test;
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 LoginPage extends Driver {
#Test
public void GoTo() {
openBrowser().get("https://wordpress.com/log-in");
}
public LoginCommand LoginAs(String username) {
return new LoginCommand(username);
}
}
I have this class SignIn:
package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import pageObject.devSplashScreenPage;
import utility.BrowserType;
import utility.Constant;
import appModule.SignIn_Action;
public class SignIn {
public WebDriver driver;
#BeforeMethod
#Parameters("browser")
public void SetUp(String browser) {
BrowserType.Execute(driver, browser);
}
#Test
public void signIn() {
// Call Sign In function
SignIn_Action.Execute(driver, Constant.StudentUsername, Constant.StudentPassword);
}
#AfterMethod
public void Teardown() {
driver.quit();
}
}
Where I am calling this code below which chooses the specific browser by the parameter that is passed. It works perfectly fine, it picks up the right browser and executes.
package utility;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BrowserType {
#Test
public static void Execute(WebDriver driver, String browser) {
// Set Browsers
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
}
else if (browser.equalsIgnoreCase("chrome")) {
{System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
driver = new ChromeDriver();
}
else if (browser.equalsIgnoreCase("ie")) {
{System.setProperty("webdriver.ie.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/IEDriverServer.exe");}
driver = new InternetExplorerDriver();
{DesiredCapabilities iecapabilities = DesiredCapabilities.internetExplorer();
iecapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);}
}
// Implicit Wait and Maximize browser
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
// Navigate to URL
driver.get(Constant.URL);
}
}
So everything executes perfectly fine in #BeforeMethod, the issue I have is the test stops because the driver doesn't pass from #BeforeMethod to #Test.
How can I get the driver that is initiated by running BrowserType.class into the #Test Sign_in.class. I guess how can i return the driver properly from browsertype and call it in Sign_in #Test.
Thanks
You should make your Execute function return the driver:
public static WebDriver Execute(String browser) {
...
return driver;
}
In your test:
public void SetUp(String browser) {
driver = BrowserType.Execute(browser);
}
Solved like this:
BrowserType.java:
package utility;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BrowserType {
#Test
public static WebDriver Execute(String browser) {
// Set Browsers
WebDriver driver = null;
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
}
else if (browser.equalsIgnoreCase("chrome")) {
{System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
driver = new ChromeDriver();
}
else if (browser.equalsIgnoreCase("ie")) {
{System.setProperty("webdriver.ie.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/IEDriverServer.exe");}
driver = new InternetExplorerDriver();
{DesiredCapabilities iecapabilities = DesiredCapabilities.internetExplorer();
iecapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);}
}
// Implicit Wait and Maximize browser
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
// Navigate to URL
driver.get(Constant.URL);
return driver;
}
SignIn.java class:
package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import pageObject.devSplashScreenPage;
import utility.BrowserType;
import utility.Constant;
import appModule.SignIn_Action;
public class SignIn {
public WebDriver driver;
#BeforeMethod
#Parameters("browser")
public void SetUp(String browser) {
driver = BrowserType.Execute(browser);
}
#Test
public void signIn() {
// Call Sign In function
SignIn_Action.Execute(driver, Constant.StudentUsername, Constant.StudentPassword);
}
#AfterMethod
public void Teardown() {
driver.quit();
}
}
The way your doing things can be greatly improved.
public class BrowserTest extends TestBase{
#Test(dataProvider="test1")
public static void execute(WebDriverHelper helper, String browser) {
// Set Browsers
driver.get(url);
Just pass the driver object (coming from the DataProvider). I assume your generating the driver instance within the DataProvider method since your test method is already parameterized and takes the driver.
public class TestBase {
private WebDriver driver;
...
#BeforeMethod
#Parameters("browser")
public void setUp(Object[] params) {
driver = (WebDriverHelper)params.get(1);
browserName = (String)params.get(2);
this.setTestName( params.get(0) + "-" + browserName;
driver.navigateTo(startUrl);
}
This code I show above wont compile but what I am trying to convey here is that you need to use the optional TestNG arg to the #BeforeMethod method, which is Object[] , and it gives you access to objects passed to test methods, BEFORE the test method is called, such as getting access to a "driver helper" created in the DataProvider factory, and then doing some Capabilities setup on that before the test is ran.
#DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new WebDriverHelper(), "firefox" },
{ "Anne", new WebDriverHelper(), "chrome"}
};
}
public class TestSuiteDriver {
private static WebDriver driver;
#BeforeClass
public static void setUp(){
System.setProperty("webdriver.chrome.driver", "/Users/Kimberleyross/chromedriver");
driver = new ChromeDriver();
}
public static WebDriver getDriver() {
return TestSuiteDriver.driver;
}
}
Hi I'm new to webdriver I'm trying to get a script to run. When I run the script it opens the browser and enters the log in details but the #Test part does not. I've tried using css locator x path etc. but nothing I've tried works. Has anyone any ideas or advice that could help?
package firsttestngpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FirstTestNGFile {
public String baseUrl = "http://zzzz";
public WebDriver driver = new FirefoxDriver();
#BeforeTest
public void login() {
driver.get(baseUrl);
WebElement id = driver.findElement(By.id("z_username"));
id.sendKeys("todd");
WebElement pass = driver.findElement(By.id("z_password"));
pass.sendKeys("todd");
WebElement button = driver.findElement(By.name("login"));
button.submit();
}
#Test
public void createSub() {
driver.findElement(By.linkText("Customers")).click();
}
#AfterTest
public void terminateBrowser() {
driver.quit();
}
}
You were missing closing } and the selector for customers element may be the reason of failure as well
package firsttestngpackage;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FirstTestNGFile {
public String baseUrl = "http://zzzz";
public WebDriver driver = new FirefoxDriver();
#BeforeTest
public void login() {
driver.get(baseUrl);
WebElement id = driver.findElement(By.id("z_username"));
id.sendKeys("todd");
WebElement pass = driver.findElement(By.id("z_password"));
pass.sendKeys("todd");
WebElement button = driver.findElement(By.name("login"));
button.submit();
}
#Test
public void createSub() {
driver.findElement(By.xpath("//*[.='Customers']")).click();
}
#AfterTest
public void terminateBrowser() {
driver.quit();
}
}