I am currently trying to automate JMeter (as an sample application) using Marathon Java Drivers. I am able to open JMeter but when i try to right click on Test Plan under the left pane, i am not able to do so. Can you please tell me what i am doing wrong. Thanks.
package javadriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Window;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import net.sourceforge.marathon.javadriver.JavaDriver;
import net.sourceforge.marathon.javadriver.JavaProfile;
import net.sourceforge.marathon.javadriver.JavaProfile.LaunchMode;
import net.sourceforge.marathon.javadriver.JavaProfile.LaunchType;
import org.openqa.selenium.support.PageFactory;
public class JavaDriverTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
JavaProfile profile = new JavaProfile(LaunchMode.JAVA_COMMAND_LINE);
profile.setLaunchType(LaunchType.SWING_APPLICATION);
profile.setMainClass("org.apache.jmeter.NewDriver");
profile.addClassPath("C:\\apache-jmeter-5.1.1\\bin\\ApacheJMeter.jar");
profile.setWorkingDirectory("C:\\\\apache-jmeter-5.1.1\\\\bin");
WebDriver driver = new JavaDriver(profile);
Window window = driver.manage().window();
window.maximize();
WebElement elementLocator = driver.findElement(By.cssSelector("label[text='Test Plan']"));
new Actions(driver).moveToElement(elementLocator, 50, 25).contextClick(elementLocator).build().perform();
//new Actions(driver).clickAndHold(elementLocator);
//new Actions(driver).contextClick(elementLocator).perform();
//driver.quit();
}
}
Looks like you are trying to get the components before the application is completely opened. So Marathon is not able to find the component.
The right click you wanted to perform is a tree item so you need to find the tree and then get node from that.
Check this link for how to locate different types of components where for Swing and JavaFX both are defined. https://marathontesting.com/marathonite-user-guide/selenium-webdriver-bindings/
Please check the following code this will give a better understanding.
package javadriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import net.sourceforge.marathon.javadriver.JavaDriver;
import net.sourceforge.marathon.javadriver.JavaProfile;
import net.sourceforge.marathon.javadriver.JavaProfile.LaunchMode;
import net.sourceforge.marathon.javadriver.JavaProfile.LaunchType;
public class JMeterTest {
private JavaDriver driver;
#BeforeTest
public void createJavaProfile_ExecJarLauncher() {
// We prefer Executable jar rather than using Java Command Line
// launcher as the application loads with a blurb and then the main
// window opens. So that we can specify the Start window from where the
// operations are performed. Other wise after creating driver you need to put sleep until the main window is opens.
JavaProfile profile = new JavaProfile(LaunchMode.EXECUTABLE_JAR);
profile.setLaunchType(LaunchType.SWING_APPLICATION);
profile.setExecutableJar("/Users/adityakarra/Projects/apache-jmeter-5.2.1/bin/ApacheJMeter.jar");
profile.setWorkingDirectory("/Users/adityakarra/Projects/apache-jmeter-5.2.1/bin");
// As the application title differs based on the Version number we have
// passed regex to match the window title.
profile.setStartWindowTitle("/^Apache JMeter.*");
driver = new JavaDriver(profile);
// Finally printing the window title after every thing is loaded.
System.out.println("Window Title ::: " + driver.getTitle());
}
#Test
public void getTreeItem() throws InterruptedException {
// As the context menu you wanted to click is a tree item. So find the
// tree initally.
WebElement tree = driver.findElementByTagName("tree");
// Now for getting the tree item we use select by properties and get the
// node.
WebElement node = tree.findElement(By.cssSelector(".::select-by-properties('{\"select\":\"/Test Plan\"}')"));
// Performing right click on the tree item
new Actions(driver).moveToElement(node, 50, 25).contextClick(node).build().perform();
// Clicking on one of the menu items in the context menu.
driver.findElementByCssSelector("menu-item[text='Disable']").click();
new Actions(driver).moveToElement(node, 50, 25).contextClick(node).build().perform();
driver.findElementByCssSelector("menu-item[text='Enable']").click();
}
#AfterTest
public void tearDown() {
if (driver != null)
driver.quit();
}
}
Note: I'm one of the contributor for Marathon.
Related
I'm trying to test a simple drag and drop behavior on a React app.
I am using the react-dnd-treeview library and their example website to test my test case.
When I run the tests in debug, I don't get any errors and Selenium is able to get the right elements, but nothing is happening, I'm not able to create or visualize any sort of actions, even after trying so many of the different answers in this similar question, but in vain.
Here is the code I am working with:
package tests;
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.interactions.Actions;
import java.io.File;
public class DAndDJava {
public static void main(String[] args) {
File file = new File("C:/misc/chromedriver.exe");
System.setProperty("webdriver.chrome.driver" , file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://teleport.github.io/react-dnd-treeview/example/dist/index.html");
WebElement dragPoint = driver.findElement(By.xpath ("//*[#id=\"root\"]/div/div/div[3]/div[2]/div[2]/div/div/div[3]/div[2]/div/div[1]/div[3]/div[1]/div"));
WebElement dropPoint = driver.findElement(By.xpath ("//*[#id=\"root\"]/div/div/div[3]/div[2]/div[2]/div/div/div[3]/div[2]/div/div[1]/div[3]/div[1]"));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(dragPoint)
.moveToElement(dropPoint)
.release(dropPoint)
.build();
dragAndDrop.perform();
driver.quit();
}
}
Could you try with below code:
Action dragAndDrop = builder.clickAndHold(dragPoint)
.moveToElement(dropPoint)
.moveByOffset(0,10)
.release()
.build()
.perform() ;
Solution is described here
react-beautiful-dnd/issue
First cursor movement should be within element borders in order to change the state of the element to dragged, and only then you can drag it to the expected position:
action.clickAndHold(elementToDrag)
.moveByOffset(0, -5)
.pause(100)
.moveByOffset(0, -300)
.release()
.perform();
I am trying to close pop up using selenium web driver with java. I have tried different ways, but unable to succeed. Please help me.
package Demo;
import java.util.Set;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class YahooTest {
public static void main(String[] args) throws InterruptedException {
FirefoxDriver obj = new FirefoxDriver();
String url = "https://www.planyourjourney.com/";
obj.get(url);
obj.manage().window().maximize();
obj.findElement(By.xpath("html/body/div[12]/div/div/div[9]")).click();
obj.findElementByClassName("dyna-link-new-registration").click();
obj.findElement(By.linkText("B2B Login")).click();
Thread.sleep(4000);
obj.findElement(By.id("userid")).clear();
obj.findElement(By.id("userid")).sendKeys("1592jet#gmail.com");
obj.findElement(By.id("ulPassword")).clear();
obj.findElement(By.id("ulPassword")).sendKeys("spyj01");
obj.findElement(By.name("Next")).click();
Thread.sleep(2000);
obj.findElement(By.id("affilitetrainadvpage"));
Alert alert = obj.switchTo().alert();
alert.dismiss();
}
}
I have seen the source code of the web page and could not find any alert in it.
The pop-up is actually written inside a div tag.
Hence, remove the codes related to alert and use below code which uses xpath.
obj.findElement(By.xpath("//*[#id='affilitetrainadvpage']/span/a/img")).click();
Main page contains more than 300 links, clicking on each link on main page opens new window (of course) with table. I always need table value from the same position, yet... sometimes (but only sometimes) that (needed) table value is actally also a link which opens new window after clicking on it.
If clicking on that table value opens new window (with new table) I need specific table value from that new window, if not (if original table value is not a link) I need only original table value.
I tried with the code below but error occured...
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[#id='aodds-info']/div[2]/table/tbody/tr[3]/td[2]"}
Command duration or timeout: 33 milliseconds
package newpackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class newdist {
public static void main(String[] args) throws IOException, InterruptedException {
// Open main page
WebDriver driver = new FirefoxDriver();
driver.get("Main page link");
Thread.sleep(5000);
// Maximize main page window
driver.manage().window().maximize();
Thread.sleep(1000);
// List off all links on Main page
List<WebElement> lista1 = driver.findElements(By.cssSelector(".first-cell.tl>a"));
// loop trough all links on Main page
for(int j=0;j<lista1.size();j++){
WebElement link = lista1.get(j);
List<WebElement> links = driver.findElements(By.cssSelector(".first-cell.tl>a"));
String homePage = driver.getWindowHandle();
link.click();
Thread.sleep(3000);
// Window handles block
Set<String>windows=driver.getWindowHandles();
Iterator iterator = windows.iterator();
String currentWindowId;
while (iterator.hasNext()){
currentWindowId = iterator.next().toString();
if(! currentWindowId.equals(homePage)){
driver.switchTo().window(currentWindowId);
Thread.sleep(3000);
// "clicking" on specific table value (clicking maybe opens new window)
driver.findElement(By.xpath(".//*[#id='sortable-1']/tbody/tr[6]/td[1]/span")).click();
Thread.sleep(3000);
// if clicking opens new window print specific value from table in that new window
try {
String s0 = driver.findElement(By.xpath(".//*[#id='aodds-info']/div[2]/table/tbody/tr[3]/td[2]")).getText();
System.out.println(s0);
}
// if clicking doesn't open new window print current table value from current window
catch (NoSuchElementException e){
String s0 = driver.findElement(By.xpath(".//*[#id='sortable-1']/tbody/tr[6]/td[1]/span")).getText();
System.out.println(s0);
}
// return to Main page
finally{
driver.close();
driver.switchTo().window(homePage);
Thread.sleep(2000);
}
}
}
}
}
}
The NoSuchElementException is imported from java.util. To catch web elements exception you need to import from org.openqa.selenium.
As a side note, using explicit and implicit wait is much better practice then using Thread.sleep.
I am testing a website with selenium webdriver.
I have imported all the jar files also, but still could not use Select Class in eclipse. It gives me an error: Select class cannot be instantiated.
I have also imported org.openqa.selenium.support.ui.Select
Following is my source code
import org.apache.bcel.generic.Select;
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.support.ui.Select;
public class Dropdown {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://www.makemytrip.com");
WebElementaddress=driver.findElement(By.xpath(".//[#id='to_typeahead1']"));
Select sc = new Select (address); // ERROR LINE
sc.selectByIndex(5);
}
}
You may try the below. Lets break it down into parts.
Select sc = new Select(driver.findElement(By.xpath("your Xpath match case")));
**//this will get the dropdown into sc object**
List<WebElement> we = sc.getOptions(); **//to get the options values into list**
System.out.println(we.size()); **//to print the size in console, this and
previous lines for debug/cross checking**
sc.selectByIndex(5); **//this will select the 5th index and 6th value(indexing starts from 0)**
Cheers!
Import org.openqa.selenium.support.ui.Select; package in your project, instead of org.apache.bcel.generic.Select;
Error will go off.
I see that the Xpath you have provided is not correct.
It should be .//*[#id='to_typeahead1']
Moreover, this element is not a select box. Its an input box.
What you can probably try is to click on the element and then try to click on the option you want to select.
I have used the org.openqa.selenium.support.ui.Select package and it worked fine.
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.support.ui.Select;
import org.testng.annotations.Test;
public class NewClassTest {
WebDriver driver = new FirefoxDriver();
#Test
public void selectOption() {
driver.get("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select");
WebElement address = driver.findElement(By.tagName("select"));
Select ab = new Select(address);
}
}
Im having some trouble uploading a file and closing the Windows explorer window. The test runs fine and i receive no errors but I the file never uploads and upload popup never closes.
ApplyJob.java
package com.example.tests;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
public class IJ_ApplyForJob {
IJ_Login login = new IJ_Login();
#Test
public void SFJ_HomeSearchBoxNotLoggedIn(WebDriver driver)
{
driver.findElement(By.id("kwdInput")).clear();
driver.findElement(By.id("kwdInput")).sendKeys("Testing");
//find the dropdown and search for the location dublin
Select ddSelectLoc = new Select(driver.findElement(By.name("Location")));
ddSelectLoc.selectByVisibleText("Dublin");
//find the dropdown and search for the Category IT
Select ddSelectCat = new Select(driver.findElement(By.id("home-category")));
ddSelectCat.selectByVisibleText("IT");
//click search button and search for term.
driver.findElement(By.className("btn-search")).click();
//click first job title
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucJobs_rptResult_ctl01_hlTitle")).click();
//click apply for job
driver.findElement(By.className("ApplyForJobButton")).click();
//complete form
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtComments")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtComments")).sendKeys("Cover Letter Testing");
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtFirstName")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtFirstName")).sendKeys("Name");
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtSecondName")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtSecondName")).sendKeys("Surname");
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtPhone")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtPhone")).sendKeys("12345678");
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtEmail")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtEmail")).sendKeys("email#gmail.com");
// Upload CV not logged in
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_fileCV")).sendKeys("C:\\Users\\Desktop\\CV_TEST.docx");
driver.findElement(By.linkText("Open")).click();
//submit application
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_btnSubmit")).click();
}
}
IJ_Test.java
package com.example.tests;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class IJ_test {
IJ_ApplyForJob afj = new IJ_ApplyForJob();
IJ_CommonSetup setup = new IJ_CommonSetup();
private WebDriver driver = new FirefoxDriver();
#Test
public void runTest() throws Exception {
setup.setUp(driver);
setup.clearCookies(driver);
afj.SFJ_HomeSearchBoxNotLoggedIn(driver);
//afj.SFJ_HomeSearchBoxLoggedIn(driver);
setup.tearDown();
}
}
Im not really sure where im going wrong so any help would be appreciated.
The problem is that I never waited for the CV to upload. #smit suggestion worked perfectly for me. By using .implicitlyWait I was able to pause the test and wait for the CV to upload.
Applyjob.java
package com.example.tests;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
public class IJ_ApplyForJob {
IJ_Login login = new IJ_Login();
#Test
public void SFJ_HomeSearchBoxNotLoggedIn(WebDriver driver)
{
driver.findElement(By.id("kwdInput")).clear();
driver.findElement(By.id("kwdInput")).sendKeys("Testing");
//find the dropdown and search for the location dublin
Select ddSelectLoc = new Select(driver.findElement(By.name("Location")));
ddSelectLoc.selectByVisibleText("Dublin");
//find the dropdown and search for the Category IT
Select ddSelectCat = new Select(driver.findElement(By.id("home-category")));
ddSelectCat.selectByVisibleText("IT");
//click search button and search for term.
driver.findElement(By.className("btn-search")).click();
//click first job title
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucJobs_rptResult_ctl02_hlTitle")).click();
//click apply for job
driver.findElement(By.className("ApplyForJobButton")).click();
//complete form
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtComments")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtComments")).sendKeys("Cover Letter Testing");
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtFirstName")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtFirstName")).sendKeys("Test");
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtSecondName")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtSecondName")).sendKeys("Test");
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtPhone")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtPhone")).sendKeys("12345678");
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtEmail")).clear();
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_txtEmail")).sendKeys("craig.mccarthy#live.ie");
// Upload CV not logged in
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_fileCV")).sendKeys("C:\\Users\\Craig\\Desktop\\CV_TEST.docx");
//wait for CV to upload
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//submit application
driver.findElement(By.id("ctl00_ctl00_cphMain_cphMain_ucApplyJob_btnSubmit")).click();
}