Selenium WebDriver upload file/close explorer window - java

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();
}

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"]

Is there any way to stop this page to directly jump on the other one?

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();
}
}

Context Click doesn't work with Marathon Java Driver

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.

Java Selenium WebDriver: Click on Instagram Login Button

I am new to coding and the Selenium WebDriver and I cannot figure out how to automate a login process for Instagram. I figured out how to input the username and password, but I was unable to figure out how to click on the login button.
Here is my code:
package com.company;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","chromedriver");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.instagram.com/accounts/login/?hl=en&source=auth_switcher");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.name("username")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.xpath("//button[contains(#class, 'loginBtn')]")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
The class for the login button is dynamic so you cannot click on it using the classname. However, you can click it using the text of the button in the xpath and I have verified it by running it myself.
You can do it like:
driver.findElement(By.xpath("//div[text()='Log In']")).click();

Taking screenshot from multiple URL's with Ashot and Selenium

I am trying to automate a test case where i have to take the screenshot of a particular screen that exists in different websites. Spcifically, i am trying to test if a particular checkbox is aligned or not.Below is what i have as my script, and i am using Ashot to take the screenshots.The scripts logs into the three systems,and click on the link i want to it to click, however there is only a single screen shot from the last URL vs a screen shot from every URL. Please help me explain how can i iterate the Ashot so that it will take a screenshot for every website instead of what it is doing right now. Essentialy all the steps are iterated except taking the screenshot, and i want the script to iterate through the screenshots as well.
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class checkboxAlignment {
String driverPath = "C:\\Users\\xxx\\Desktop\\Work\\chromedriver.exe";
public WebDriver driver;
public String expected = null;
public String actual = null;
#BeforeTest
public void launchBrowser() {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
}
#Test(dataProvider = "URLprovider")
private void notePrice(String url) throws IOException {
driver.get(url);
System.out.println(driver.getCurrentUrl());
WebElement email = driver.findElement(By.xpath("//input[#id='Email']"));
WebElement password = driver.findElement(By.xpath("//input[#id='PWD']"));
WebElement submit = driver.findElement(By.xpath("//button[#type='submit']"));
email.sendKeys("xxx#xxx.com");
password.sendKeys("xxx");
submit.click();
System.out.println(driver.getTitle());
driver.manage().window().maximize();
//click on the PI tab
driver.findElement(By.id("IDpi")).click();
// This doesnot iterate, only one screenshot is taken by Ashot
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"PNG",new File("C://Users//dir//eclipse-workspace//someDir//screenshots//checkbox.jpg"));
}
#DataProvider(name = "URLprovider")
private Object[][] getURLs() {
return new Object[][] { { "http://www.someURL.com/A" }, { "http://www.someurl.com/B" },
{ "http://www.someurl.com/C" } };
}
}
You are saving all the screenshot in the same file checkbox.jpg. That is why your previous screenshots are replaced by the last one. Try to name the file different for every screenshot. Also, save the screenshots with .png extension as that is the actual file type.
Try this for saving the image:
ImageIO.write(fpScreenshot.getImage(),"PNG",new File("C://Users//dir//eclipse-workspace//someDir//screenshots//checkbox-"+driver.getCurrentUrl()+".png"));
I'm doing something like this
#Step("Захват страницы для хранилища")
protected void capturePageToVault(String pageName, String url, int scrollTime) throws IOException {
open(url);
expected = capturePage(scrollTime);
ImageIO.write(expected.getImage(), "png", expectedImg(pageName));
attach = new FileInputStream(expectedImg(pageName));
Allure.addAttachment("Exemplar", "image/png", attach, ".png");
attach.close();
}

Categories

Resources