Taking screenshot from multiple URL's with Ashot and Selenium - java

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

Related

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

Need to verify user landed on correct page after login?

I'm testing this on my local system and trying to verify user is successfully logged in and landed on correct page.
Getting error when trying to compare String data with webelement data.
`package wnsautomation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class login {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\orange\\Downloads\\geckodriver.exe");
driver= new FirefoxDriver();
WebDriverWait myWait = new WebDriverWait(driver, 10);
String baseUrl = "http://192.168.1.52:9000";
driver.get(baseUrl);
myWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")));
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input")).sendKeys("admin#gmail.com");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[3]/div/div/input")).sendKeys("8JXzwRs4VWeGP0Sy");
driver.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[5]/button")).click();
String expectedtext="Summary";
WebElement actualtext;
actualtext = driver.findElement(By.linkText("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3"));
if (actualtext.contentEquals(expectedtext)){
System.out.println("User succesfully loggedIN");
} else {
System.out.println("Invalid credtendials!!");
}
}
}`
Instead of using Webelement, use String with getText()method so, it will give you the actual String. Not the Webelement.
Note: Instead of using absolute xpath, use relative xpath. For more details on xpath tutorial, refer this link.
String expectedtext="Summary";
String actualtext = driver.findElement(By.xpath("//div/h3[text()='Summary']")).getText();
System.out.println(actualtext);
if (expectedtext.equalsIgnoreCase(actualtext))
{
System.out.println("User succesfully loggedIN");
}
else
{
System.out.println("Invalid credtendials!!");
}
Try this-
String expectedtext="Summary";
String actualtext= null;
actualtext = driver.findElement(By.xpath("/html/body/div[3]/div/ng-include/div/div/div[1]/div/h3")).gettext();

How to get drag and drop working with Selenium WebDriver

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.

Handling Light box / Light window in selenium using webdriver

I have been trying to automate a certain flow on a website, but whenever I navigate to the site a light box/window appears because of which my element is not getting selected.
I have tried 2 approaches to close the window but none of them are working:
Have tried to close the window using the pop up closing approach.
Have tried Frames approach but that isn't working as well.
Below is my code:
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
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.firefox.FirefoxDriver;
public class Handle_Windows_popUP {
static WebDriver driver = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\Drvier\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.makemytrip.com");
Set<String> id = driver.getWindowHandles();
Iterator<String> itr = id.iterator();
System.out.println(id.size());
while(itr.hasNext())
{
Object element = itr.next();
System.out.println("id: "+element);
}
// Trying to find the 'X' button if present in any of the frame but none of the frame has it
List<WebElement> ls = driver.findElements(By.tagName("iframe"));
System.out.println("Numberof frames:"+ls.size());
for(int i=0;i<ls.size();i++)
{
driver.switchTo().frame(i);
System.out.println("Frame: "+i);
System.out.println(driver.findElements(By.xpath("*[#id='htmlDoc']/body/div[13]/div/a[1]")).size());
driver.switchTo().defaultContent();
}
// The Pop-up approach
String parent_Window = itr.next();
String child_win = null;
while(itr.hasNext())
{
child_win = itr.next();
driver.switchTo().window(child_win);
driver.close();
}
driver.switchTo().window(parent_Window).getTitle();
}
}
as this works fine if u refresh the browser, i will suggest u not to use any other code. But if u want to know how to close the window without refresh browser, write the below code after launch:
//wait until the browser loaded.
//than use this code
driver.findElement(By.cssSelector("div.appfest_container.appfest_container-bg.visible-md.visible-lg >a.appfest_container-close.pull-right.clearfix")).click();
my css path is long but u may change it by using xpath.
//*[#id="htmlDoc"]/body/div[13]/div/a[1]
and hope u will accept the answer.

Selenium WebDriver upload file/close explorer window

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

Categories

Resources