WebDriver driver;
String baseUrl = "http://example.com";
#BeforeClass
public void beforeClass() {
System.setProperty("webdriver.firefox.marionette","local path to geckodriver-v0.10.0-win64");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver();
}
#Test
public void login() throws InterruptedException {
driver.get(baseUrl);
Thread.sleep(3000);
}
#AfterClass
public void afterClass() {
}
this code I am trying to run using TestNG tool but it's failing.
It runs perfectly when using it as Java Application but I want to run this script as a TestNG script
Related
WebDriver driver = new ChromeDriver(); //Launch the chrome browser
driver.get("https://www.seleniumeasy.com/test/drag-and-drop-demo.html");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id=\"todrag\"]/span[2]"));
WebElement from = driver.findElement(By.xpath("//*[#id=\"todrag\"]/span[2]"));
WebElement to = driver.findElement(By.xpath("//*[#id='mydropzone']"));
Actions builder = new Actions(driver);
builder.dragAndDrop(from, to).perform();
I put it in Junit test. Works fine for me.
But I noticed you're not setting property to use a local chromedriver.
Have you got a chromedriver downloaded?
private WebDriver driver;
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver",
//"C:/path/to/your/chromedriver.exe"
"/path/to/your/chromedriver"); // Might be this.
driver= new ChromeDriver();
}
#After
public void tearDown() throws Exception {
driver.quit();
}
#Test
public void testDragNDrop() {
driver.get("https://www.seleniumeasy.com/test/drag-and-drop-demo.html");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id=\"todrag\"]/span[2]"));
WebElement from = driver.findElement(By.xpath("//*[#id=\"todrag\"]/span[2]"));
WebElement to = driver.findElement(By.xpath("//*[#id='mydropzone']"));
Actions builder = new Actions(driver);
builder.dragAndDrop(from, to).perform();
}
This may be as simple as getting a variable from another class. I am still learning Java and Selenium.
I would like the test run report (ExtentReports) to be able to report the browser at a #Test level (capabilities). Currently Grid runs the same tests on different browsers, and the report does not distinguish them.
Using Selenium Grid, I define my #Test's Capabilities (including browser) with #BeforeMethod. I do this in my BaseTest class.
public class BaseTest {
#BeforeMethod(alwaysRun = true)
#Parameters({ "platform", "browser", "version" })
public void setup(String platform, String browser, String version)
throws MalformedURLException, InterruptedException {
RemoteWebDriver driver = null;
//important: Thread local!
threadedDriver = new ThreadLocal<RemoteWebDriver>();
DesiredCapabilities caps = new DesiredCapabilities();
// Platforms
if (platform.equalsIgnoreCase("windows"))
caps.setPlatform(Platform.WINDOWS);
if (platform.equalsIgnoreCase("XP"))
caps.setPlatform(Platform.XP);
if (platform.equalsIgnoreCase("WIN8"))
caps.setPlatform(Platform.WIN8);
if (platform.equalsIgnoreCase("WIN8_1"))
caps.setPlatform(Platform.WIN8_1);
if (platform.equalsIgnoreCase("ANY"))
caps.setPlatform(Platform.ANY);
if (platform.equalsIgnoreCase("MAC"))
caps.setPlatform(Platform.MAC);
if (platform.equalsIgnoreCase("Android"))
caps.setPlatform(Platform.ANDROID);
// Browsers
if (browser.equalsIgnoreCase("Internet Explorer"))
caps.setBrowserName("internet explorer");
if (browser.equalsIgnoreCase("Firefox"))
caps.setBrowserName("firefox");
if (browser.equalsIgnoreCase("chrome"))
caps.setBrowserName("chrome");
if (browser.equalsIgnoreCase("MicrosoftEdge"))
caps.setBrowserName("MicrosoftEdge");
if (browser.equalsIgnoreCase("iPad"))
caps.setBrowserName("ipad");
if (browser.equalsIgnoreCase("iPhone"))
caps.setBrowserName("iphone");
if (browser.equalsIgnoreCase("Android"))
caps.setBrowserName("android");
// Version
caps.setVersion(version);
System.out.println(caps);
System.out.println(browser);
//Initialize driver with capabilities
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);
//this uses below methods to set above RemoteWebDriver to the getDriver()
//method in a threaded instance.
setWebDriver(driver);
initialize();
}
}
So now I have a browser variable local to each threaded Grid test run. I need to get that variable into each #Test method. Here is my #Test in a separate class. At the beginning of the try statement I would like to print the browser variable for the current threaded Grid test capabilities
public class Workflow1 extends BaseTest {
#Test
public void Workflow1TestInvalidPolicyNumbers() throws InterruptedException {
HomePage homePage = new HomePage(getDriver());
ExtentTest testReporter = ComplexReportFactory.getTest();
try {
System.out.println("This is the browser:" + ??(help here)??);
loginMethod("TestUser","TestPassword");
homePage.setFindAPersonOrPolicySearchField("1234");
homePage.clickSearchButton();
testReporter.log(LogStatus.INFO, "Searched \"1234\"");
Thread.sleep(2000);
if (getDriver().getPageSource().contains("Policy numbers should be 7 or 10 digits long"))
testReporter.log(LogStatus.PASS, "Policy numbers should be 7 or 10 digits long");
else
testReporter.log(LogStatus.FAIL, "Results incorrect" + testReporter.addScreenCapture(ComplexReportFactory.CaptureScreen(getDriver())));
} catch (Exception e) {
testReporter.log(LogStatus.ERROR, "Exception found: " + e.getMessage()
+ testReporter.addScreenCapture(ComplexReportFactory.CaptureScreen(getDriver())));
System.out.println(e);
}
}
It looks like the browser information is passed into the BaseTest.java class in the setup() method.
You could store this data in a variable which would then be available to all dependant classes:
public class BaseTest {
protected String browser; // add a property to hold this value
#BeforeMethod(alwaysRun = true)
#Parameters({ "platform", "browser", "version" })
public void setup(String platform, String browser, String version)
throws MalformedURLException, InterruptedException {
this.browser = browser; // store the given browser string
RemoteWebDriver driver = null;
//important: Thread local!
threadedDriver = new ThreadLocal<RemoteWebDriver>();
DesiredCapabilities caps = new DesiredCapabilities();
// Platforms
if (platform.equalsIgnoreCase("windows"))
caps.setPlatform(Platform.WINDOWS);
And then the BaseTest subclasses could reference it directly:
public class Workflow1 extends BaseTest {
#Test
public void Workflow1TestInvalidPolicyNumbers() throws InterruptedException {
HomePage homePage = new HomePage(getDriver());
ExtentTest testReporter = ComplexReportFactory.getTest();
try {
System.out.println("This is the browser:" + browser); // then retrieve it
You can also get the browser flavor name and a whole bunch of information by querying the RemoteWebDriver object itself to reveal the actual capabilities by invoking
getDriver().getCapabilities().getBrowserName()
This does away with the need to even have the browser flavor as a separate data member in the test class.
See here for javadocs.
I test safari browser on Windows with Selenium Webdriver, when in debug mode, my script works well, but when I run it, it could not work well. Does anyone encounter this situation?
public class JustForTestSafari {
public WebDriver driver;
#Test
public void f() {
driver = new SafariDriver();
String baseURL = "http://universitytest.emersonprocess.com/";
String expectedTitle = "ProcessWorld - Your Connection to Global Information";
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get(baseURL);
driver.manage().window().maximize();
String actulTitle = driver.getTitle();
Assert.assertEquals(expectedTitle, actulTitle);
driver.findElement(By.id("_ctl0_mastercontent_username")).clear();
driver.findElement(By.id("_ctl0_mastercontent_username")).sendKeys("***.guo");
driver.findElement(By.id("_ctl0_mastercontent_password")).clear();
driver.findElement(By.id("_ctl0_mastercontent_password")).sendKeys("*******");
driver.findElement(By.id("_ctl0_mastercontent_btn_standardsignin")).click();
}
I am not having any luck getting Phantomjs to access a url in my Selenium webdriver tests. I continue to get the following error after the assert -
Expected :Login
Actual :403 - Forbidden: Access is denied.
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at
Here is my - Also HTML Unit Driver does the same thing. However when I use Firefox driver it runs fine.
public class EasyElectPhantomTest {
private WebDriver driver;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private String baseUrl;
#Before
public void setUp() throws Exception {
driver = new PhantomJSDriver();
baseUrl = "https://secure02.pilot.principal.com/";
#Test
public void testEasyElectPhantom() throws Exception {
driver.get(baseUrl + "/login?token=ODEFIJMLEHGIOPRNGIYN");
driver.manage().window().maximize(); //Maximizing window
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
assertEquals("Login", driver.getTitle());
driver.findElement(By.id("firstName")).sendKeys("Ryan");
I am running tests using two Firefox Drivers to display the results. I am using two screens on my computer. Can I automatically open a driver on each screen via java code running in Eclipse?
Thanks.
I think you should run parallel test cases https://saucelabs.com/java/se2/8
#RunWith(Parallelized.class)
public class WebDriverParallelTest {
private String browser;
private String os;
private String version;
public WebDriverParallelTest(String os, String version, String browser) {
super();
this.os = os;
this.version = version;
this.browser = browser;
}
#Parameterized.Parameters
public static LinkedList browsersStrings() throws Exception {
LinkedList browsers = new LinkedList();
browsers.add(new String[]{Platform.XP.toString(), "17", "firefox"});
//add any additional browsers here
return browsers;
}
private WebDriver driver;
#Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, browser);
capabilities.setCapability(CapabilityType.VERSION, version);
capabilities.setCapability(CapabilityType.PLATFORM, os);
this.driver = new RemoteWebDriver(
new URL("http://credential of sauce lab"), capabilities);
}
#Test
public void webDriver() throws Exception {
driver.get("http://www.amazon.com/");
assertEquals("Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more", driver.getTitle());
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}