Passing String from Jenkins to Java code - java

My framework consists of TestNG + Cucumber +Jenkins , I'm running the job using bat file configuration in jenkins.
My doubt is , I have a class file to launch the browser and I pass string value to if loop saying ,
if string equals "chrome" then launch the Chrome browser and soon.
Is there a way to pass the chrome value from jenkins into class file ?
example :
public class launch(){
public static String browser ="chrome"
public void LaunchBrowser() throws Exception{
if (browser.equalsIgnoreCase("chrome"))
{
launch chrome driver
}
}
Now i would like to pass the static string value from jenkins ,
Help is appreciated.
Thanks in advance.

You can do something like below
public class Launch {
//You would be passing the Browser flavor using -Dbrowser
//If you don't pass any browser name, then the below logic defaults to chrome
private static String browser =System.getProperty("browser", "chrome");
public void LaunchBrowser() throws Exception {
if (browser.equalsIgnoreCase("chrome")) {
//launch chrome driver
}
}
}

Related

Intellij IDE throws error while trying to use Extent Report in Java, Selenium, TestNG environment

I am trying to add ExtentReport in my test automation project, but IntelliJ is throwing this error at line "report = ..." inside "BeforeMethod"
Expected 0 arguments but found 1
I am trying to insert the location of the report file. Help much appreciated. Thanks.
public class Methods {
public static WebDriver driver;
public static ExtentReports report;
public static ExtentTest test;
#BeforeTest
public void startReport(){
report = new ExtentReports(System.getProperty("C:\\Program Files (J)\\VCWebMaine\\Report.html"));
}
#BeforeMethod
public void setDriver() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (J)\\VCWebMaine\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://xxxxxxxxxxxxxxxx");
Thread.sleep(2000);
}
I tried looking for answers in Google. It looks like the same lines work in Eclipse. Is it a problem in the IDE?
Other part of the script as below:
public class TestCases extends Methods {
ExtentTest test = Methods.test;
#Test
public void scenario01() throws InterruptedException, IOException {

How to resolve the issue with the browser is being closed when start running testng using Edge driver

I have a couple issues when I run the test with edge web browser.
The first issue is that every time I run the test by run as > testng test , the automation will close all the existing open edge web browser and then it will open a brand new edge browser but it is blank (could not load the url)
The second issue is that the automation would only load the url if I manually close all of the existing edge browser.
Is there a way to fix these issues? please help me thank you in advance.
Here is my code
public class OpenThePageUsingEdge {
public String baseUrl = "https://catalog-qa.baylorgenetics.com/search";
String driverPath = "C:\\Eclipse\\MicrosoftWebDriver.exe";
public WebDriver driver ;
//initiate NameofInsured as the GenerateData class
//GenerateData NameOfAddrecipients;
#BeforeTest
public void setup(ITestContext ctx) {
TestRunner runner = (TestRunner) ctx;
runner.setOutputDirectory("J:\\zzQA Selenium Automation Suite\\Test Results");
}
#Test
public void openThePageusingEdge () throws InterruptedException {
System.out.println("launching Edge browser");
System.setProperty("webdriver.edge.driver", driverPath);
driver = new EdgeDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

How to use browser variable defined in page factory is used in hook file

I am using cucumber with POM.I have created a file called "Driver factory" where i have initialised a String browserName as a parameter to public static void openBrowser(String browserName). (Because i want to test in multiple browser).I am trying to call method in hooks file df.openBrowser(browserName);
public static void openBrowser(String browserName) throws Exception{
if(browserName.equalsIgnoreCase("chrome")) {
System.out.println("***Test cases are executing in Chrome****");
System.setProperty("webdriver.webdriver.chrome.driver.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
I am getting error in Hooks.java file saying "browserName cannot be resolved to a variable".
How do I resolve this?

Why is my selenium webdriver opening blank windows of "data;"

I'm trying to set up my webdriver so that it can execute tests in parallel from the xml sheet, this it does, but I find sometimes it opens up blank chrome windows of "data;"
I've researched around other questions and all the answers seem to say things about putting the webdriver as a new threadlocal<RemoteWebDriver>, which I am already doing.
This is the WebDriverSetup.java file I am using:
public class WebDriverSetup {
private static ThreadLocal<RemoteWebDriver> threadDriver = null;
public WebDriver driver(){
return threadDriver.get();
}
#BeforeMethod
public void setUp() throws MalformedURLException {
//Set up the path to the chromedriver so that the user will not have problems if they don't have the system path variable set up.
System.setProperty("webdriver.chrome.driver", TestExecutor.projectDirectory + "/chromedriver.exe");
//Set the hub URL
String seleniumUrl = "http://" + TestExecutor.hubUrl + "/wd/hub";
//Turn off logging because as of selenium-standalone-server-3.0.1, there
//is an "INFO" message that appears in console that could be mistaken for
//an error message.
Logger.getLogger("org.openqa.selenium.remote").setLevel(Level.OFF);
//threadDriver needs to be on its own local thread
threadDriver = new ThreadLocal<RemoteWebDriver>();
//Set chromeoptions so they open headless on the VM, but the VM imagines the
//tests as if chrome was running full screen on a desktop session.
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1920,1080");
//Apply the options to the chromedriver session.
URL seleniumHubURL = new URL(seleniumUrl);
threadDriver.set(new RemoteWebDriver(seleniumHubURL, options));
}
}
and this is how I am calling it from my test:
public class Demo_1_Filter_Users_By_Firstname extends TestBase.ClassGlobals
{
private WebDriverSetup webDriverSetup;
private EventFiringWebDriver driver;
private File logFile;
#Test
public void main(){
//The main method
driver.get("web_application_url");
}
#BeforeMethod
public void testSetup() throws IOException {
//Setup the webdriver
webDriverSetup = new WebDriverSetup();
driver = new EventFiringWebDriver( webDriverSetup.driver() );
//Set up an eventhandler on the event so that all the logging functions work
EventHandler handler = new EventHandler();
driver.register( handler );
//Setup the logfile
logFile = commonMethods.newLogFile();
//Log
commonMethods.log(logFile, "------TEST STARTED------");
}
#AfterMethod
public void testClosure(){
//Close webdriver session, log test done etc
}
}
The error that I am experiencing doesn't happen every time, and I don't understand why the window is hanging on data; even though the first line of my main method is to create a new webdriver session, and then using that session, open the web application through driver.get()
I am using chromedriver version 2.41
The latest stable version of ChromeDriver (88.0.4324.96) appears to have fixed this:
Resolved issue 3641: Page not getting loaded/rendered when browser window is not in focus with Chrome Beta v87 and chromedriver v(87/86)
I've been having this issue for a while and updating my chromedriver.exe file finally solved it.

Illegal Argument Exception in Appium framework

After (foolishly) upgrading Appium Server along with various project libraries, such that I cannot tell which, if any, caused the problem, my previously running Appium framework suddenly started failing upon attempting to locate any elements.
The server starts (either manually via desktop or through the java code) and it launches my emulator (if not already loaded), makes the connection, opens the app (in the case show, simply settings) and fails as soon as it tries to validate that the settings main page is displayed by checking the existence of the "Settings" text:
Given the settings app is displayed (FAILED)
(java.lang.IllegalArgumentException: Can not set
io.appium.java_client.android.AndroidElement field
com.mindtree.pageobjects.SettingsMainPage.header to
org.openqa.selenium.remote.RemoteWebElement$$EnhancerByCGLIB$$d27c0df4)
The Appium server versions for both desktop and nodeJS is currently 1.7.2 I believe the problem started when I was originally at 1.7.1 or 1.7.2 and it succeeded in doing an auto-update on the desktop version to 1.8.1.
Selenium version is 3.11.0 (tried various flavors from 3.9.0 through 3.13.0)
Appium Java client is 6.0.0-BETA5 (tried 6.0.0-BETA4, 6.0.0, 6.1.0)
Java is 1.8
The JBehave test step that reports the error:
#Given("the settings app is displayed")
public void givenTheSettingsAppIsDisplayed() {
main = new SettingsMainPage(driver);
if (main.pageLoaded())
test.logGivenPass("the settings app is displayed");
else {
test.logGivenFail("the settings app is displayed");
fail();
}
}
The corresponding page object snippet:
public class SettingsMainPage extends MobilePageObject {
public SettingsMainPage(AndroidDriver<AndroidElement> driver) {
super(driver);
System.out.println("Settings Main page class has been initialized");
}
#AndroidFindBy(xpath = "//android.widget.TextView[#text='Settings']")
AndroidElement header;
#AndroidFindBy(id= "android:id/title")
List<AndroidElement> titles;
#AndroidFindBy(id= "android:id/summary")
List<AndroidElement> summaries;
public Boolean pageLoaded() {
return helper.isDisplayed(header);
}
}
Googling this particular error returns a few hits, but no offered solutions.
Any guidance appreciated.
edit: I should add that the failure seems to happen upon initialization of the page object via the page factory, since the text "initialized" is never shown, it fails while trying to initialize all the page elements, specifically the first one, at least according to the error message.
My base page object is below:
import java.time.Duration;
import org.openqa.selenium.support.PageFactory;
import com.mindtree.helpers.AppiumUtils;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
public class MobilePageObject {
AndroidDriver<AndroidElement> driver;
AppiumUtils helper;
public MobilePageObject(AndroidDriver<AndroidElement> driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver, Duration.ofSeconds(15)), this);
helper = new AppiumUtils();
}
}
Edit Update: Downgraded the Appium Server through NodeJS from 1.7.2 to
1.7.1. Result: no change, same error reported.
I am using Appium server 1.8.1, selenium 3.13.0 and java client 6.1.0. I use the page object model like following and it works fine.
public class SettingsMainPage{
public SettingsMainPage(AndroidDriver<AndroidElement> driver) {
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
System.out.println("Settings Main page class has been initialized");
}
#AndroidFindBy(xpath = "//android.widget.TextView[#text='Settings']")
AndroidElement header;
#AndroidFindBy(id= "android:id/title")
List<AndroidElement> titles;
#AndroidFindBy(id= "android:id/summary")
List<AndroidElement> summaries;
public boolean pageLoaded() {
try{
(new WebDriverWait(driver, 20)).until(ExpectedConditions.visibilityOfElementLocated(header));
return header.isDisplayed();
}
catch(Exception e){
return false;
}
}
}
And you must define your desiredCapabilities like following:
public static AppiumDriver<MobileElement> driver;
public static AppiumDriver<MobileElement> setupDesiredCapabilities(String appPackage, String appActivity,
String udid, String platformVersion, boolean noReset) {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Android phone"); //any name
caps.setCapability("udid", udid);
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", platformVersion);
caps.setCapability("appPackage", appPackage);
caps.setCapability("appActivity", appActivity);
caps.setCapability("noReset", noReset); //optional
try {
driver = new AndroidDriver<MobileElement>(new URL(
"http://127.0.0.1:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
//
} catch (Exception ex) {
//
}
return driver;
}
Make sure you have define static AppiumDriver and use the same
driver object to call constructor of each page.

Categories

Resources