Automating responsive design using Selenium Webdriver - selenium-webdriver

Is it possible to automate responsive design testing using Selenium Webdriver? Can it be done with chrome options or a library of some sort?

Try the following sample of Java Code with JUnit:-
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ResponsiveWebTest {
public WebDriver driver;
public List<Dimension> screenDimensionsList;
#Before
public void beforeTestMethod(){
// create list of dimensions for various screen sizes
screenDimensionsList = new ArrayList<Dimension>();
screenDimensionsList.add(new Dimension(1600,800));
screenDimensionsList.add(new Dimension(1200,800));
screenDimensionsList.add(new Dimension(992,800));
screenDimensionsList.add(new Dimension(768,800));
screenDimensionsList.add(new Dimension(480,800));
screenDimensionsList.add(new Dimension(360,800));
// initialize the driver for browser
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://nandal.in");
}
#Test
public void testVariousScreenSizes(){
for(Dimension d: screenDimensionsList){
driver.manage().window().setSize(d);
// run some test cases for this screen size
// some test case steps
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
This will open chrome browser with given url, and then resizes the browser according to the dimensions list to test the responsive behavior of the webpage, You can add your test logic there.

Related

Test annotation code is not working, just BeforeMethod is working

My code is working until #BeforeMethod and it is opening the URL, but what I'm typing after #Test is not working. It is not typing credentials on the login page, hence Test failed.
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Great {
WebDriver driver;
#BeforeMethod
public void setup()
{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files
(x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseurl = "https://getfieldforce.com/dishqa";
driver.get(baseurl);
// Actions act = new Actions(driver);
}
#Test (priority=0)
public void login()
{
System.out.println("Login process starts");
driver.findElement(By.id("email")).sendKeys("123#p.com");
driver.findElement(By.id("password")).sendKeys("123456");
driver.findElement(By.id("cta")).click();
System.out.println("Login Sucessfully");
}
Removing webdriver from this line fixed the issue; WebDriver driver = new ChromeDriver();
Thanks for looking into this :-)

Trying to run chromedriver for Selenium results in errors

I used this code:
public class ChromeTest {
#Test
public void LaunchChrome_Method2() {
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
}
}
from this website:
http://www.automationtestinghub.com/selenium-chromedriver/
There are errors in your code. This can be fixed by the following:
Your filename is Chrom.java hence your public class name must be Chrom not ChromeTest.
Add import statements for WebDriver and #Test and ChromeDriver.
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

i am unable to type text in loginpage by using sendkeys

I am unable to type text in login page, tried all the solutions provided online but issue still exists. anyone help me to resolve the issue. below is the code:
package com.individual.newlearn;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class secondProgram {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/webhp?ie=UTF-8&rct=j&gws_rd=cr&dcr=0&ei=9Wq2WozGFIH4vgSJ2LmoAw");
Thread.sleep(2000);
driver.findElement(By.xpath("//a[text()='Gmail']")).click();
//driver.navigate().to("https://accounts.google.com/signin/v2/identifier?hl=EN&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
driver.findElement(By.xpath("//a[text()='Sign In']")).click();
Thread.sleep(2000);
driver.findElement(By.id("identifierId")).sendKeys("mail id");
java script i used for typing mail id:
/*WebElement searchbox = driver.findElement(By.id("identifierId"));
JavascriptExecutor myExecutor = ((JavascriptExecutor)driver);
myExecutor.executeScript("arguments[0].value='mail id';", searchbox);
Thread.sleep(3000);*/

How do I automate testing for hybrid apps which has it's web view resources from assets?

I am using Appium for testing my Android Hybrid App.
What parameter should I give for the RemoteWebDriver constructor? I tried giving the html file path from the internal storage, but it's not working.
Any help would be appreciated.
package firsttestngpackage;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class FirstTestNGFile {
WebDriver driver;
#BeforeClass
public void setup() throws MalformedURLException{
DesiredCapabilities capabilities=new DesiredCapabilities();
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("platformVersion", "7.1.1");
capabilities.setCapability("deviceName", "MotoG5splus");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("app", "C:\\Users\\chandrahas\\Downloads");
driver=new RemoteWebDriver(new URL(""),capabilities);
capabilities.setCapability("appPackage", "com.reliance.agencydev");
}
#Test
public void testirm()throws Exception {
driver.findElement(By.xpath("//*[#id=\"userName\"]/input")).sendKeys("9642039085");
driver.findElement(By.xpath("//*[#id=\"password\"]/input")).sendKeys("password");;
}
}
Where is your Appium server running? If it's on your local machine, try with:
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub/"), capabilities);
Also try moving the line where you declare appPackage capability to before you create your driver.

Openbravo ERP application using selenium webdriver but not able to find any web element

When I'm trying to run this code, I am unable to find any web element
package PremisesManagement;
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.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class openbravo {
#Test
public void openbravoLoginTest() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
WebDriver d = new ChromeDriver();
d.get("http://119.81.222.91:8080/camps/security/Login_FS.html");
Thread.sleep(3000);
d.findElement(By.id("user")).sendKeys("xyz");
WebDriverWait wait = new WebDriverWait(d, 120);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("//button[#id='buttonOK']")));
d.findElement(By.xpath("//button[#id='buttonOK']")).click();
}
}
Actually located elements are inside a frame with id paramFrame1, So you need to switch this frame before finding element as below :-
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://119.81.222.91:8080/camps/security/Login_FS.html");
WebDriverWait wait = new WebDriverWait(driver, 10);
//First switch to frame with id paramFrame1
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("paramFrame1"));
//Now find the element
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user"))).sendKeys("xyz");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("buttonOK"))).click();

Resources