I use selenium-jupiter. I am getting a webdriver from method arguments like this:
#Test
public void testWithChrome(ChromeDriver chromeDriver) {
chromeDriver.get("someUrlHere");
}
Now I want to run tests on grid so I need to use webdriver based on environment. For example when developing tests on my PC I want to use (local) ChromeDriver, but when running tests on grid with Jenkins, I want to use RemoteDriver.
So I need something like this: (That gives me local Chrome when env = 0 or gives me remote Chrome when env = 1 but it's not working)
int env = 0;
#Test
public void testWithChrome(
(env == 0 ? ChromeDriver driver : RemoteDriver driver)) {
driver.get("someUrlHere");
}
In short: When configuring your Selenium extension programmatically you can force usage of a Selenium Grid by configuring its URL as follows (using JUnit 5 annotations):
abstract class UiTest {
#RegisterExtension
static SeleniumExtension seleniumExtension = new SeleniumExtension();
#BeforeAll
static void setUpOnce() {
boolean isRunningInCiEnvironment = ...
if( isRunningInCiEnvironment ) {
// this will force Selenium Jupiter to use a RemoteWebDriver
seleniumExtension.getConfig().setSeleniumServerUrl("http://...");
}
// without above condition, a FirefoxDriver will be used locally
seleniumExtension.addBrowsers(BrowserBuilder.firefox().build(););
}
}
class MyTest extends UiTest {
// Use WebDriver interface in test method: concrete browser detected
// at execution time (via #BeforeAll, inherited from parent class)
#Test
void my_test_Case(WebDriver webDriver) {
webDriver.get(...)
Assert.(...)
}
}
The problem in length is decribed here.
I think what would be better here is to have a method that is executed before any test (annotated with #BeforeAll) that determines what environment the script is being run in. It probably reads from some config file local vs grid. Once that is determined, assign the driver variable either an instance of ChromeDriver or RemoteDriver. From then on, your tests will pass around the driver instance which will be of type WebDriver because both ChromeDriver and RemoteDriver inherit from it.
WebDriver driver;
#BeforeAll
public void setup()
{
// read from config file, etc. to determine if local or grid
if (local)
{
driver = new ChromeDriver();
}
else
{
driver = new RemoteDriver();
}
}
#Test
public void test()
{
driver.get("someUrlHere");
}
You can do that with WebDriverManager that comes with this extension.
#BeforeEach
public void setUp()
{
switch(browser)
{
case "chrome" ->
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
case "firefox" ->
{
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
case "edge" ->
{
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
}
}
driver.manage().window().maximize();
}
Related
Am trying to zoom out of my current page as some of the monitors are quite small there our application is not loading properly.
Selenium 3.141.59
Java 1.8
This is my sample code:
public class ScreenResolutionCheck {
static WebDriver driver = null;
public static void main(String[] args) {
int width = 1512, height = 982; //Default Macbook Pro Size
WebDriverManager webDriverManager = WebDriverManager.chromedriver().browserInDocker().enableVnc().linux().dockerScreenResolution(width + "x" + height +"x24").timeout(100);
driver = webDriverManager.create();
BaseFunctions.logInfoWithOutScreenShot("VNC Url",webDriverManager.getDockerNoVncUrl().toString());
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.COMMAND, Keys.SUBTRACT));
findElement(By.id("txtUsername")).sendKeys("Admin");
findElement(By.id("txtPassword")).sendKeys("admin123");
findElement(By.id("btnLogin")).click();
findElement(By.id("menu_admin_viewAdminModule")).click();
findElement(By.id("tableWrapper"));
driver.quit();
webDriverManager.quit();
}
public static WebElement findElement(By by) {
WebElement we = new WebDriverWait(driver, 30).until(d -> d.findElement(by));
return we;
}
}
I tried the following:
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.COMMAND,Keys.SUBTRACT));
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.COMMAND,Keys.SHIFT,Keys.SUBTRACT));
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.COMMAND,"-"));
new Actions(driver).sendKeys(driver.findElement(By.tagName("body"),Keys.chord(Keys.COMMAND,Keys.SUBTRACT)).build().perform();
js.executeScript("arguments[0].dispatchEvent(new KeyboardEvent('keydown',{'key':'Meta'}))",we);
js.executeScript("arguments[0].dispatchEvent(new KeyboardEvent('keydown',{'key':'+'}))",we);
js.executeScript("arguments[0].dispatchEvent(new KeyboardEvent('keyup',{'key':'Meta'}))",we);
js.executeScript("arguments[0].dispatchEvent(new KeyboardEvent('keyup',{'key':'+'}))",we);
options.addArguments("force-device-scale-factor=0.75");
options.addArguments("high-dpi-support=0.75");
WebDriver driver = new ChromeDriver(options);
Only this one worked but its not optimal as the moment I navigate to next screen, the zoom gets resetted.
js.executeScript("document.body.style.zoom='80%'");
I followed all these links but none worked for me. Any help would be much appreciated.
StackOverflowQuestion
Also Robot class doesn't suit me as I had to run in different OSes.
Coud anybody help me fix my problem? When I tried to run jbpm sample in eclipse. This is code:
public class ProcessMain {
public static void main(String[] args) {
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieBase kbase = kContainer.getKieBase("kbase");
RuntimeManager manager = createRuntimeManager(kbase);
RuntimeEngine engine = manager.getRuntimeEngine(null);
KieSession ksession = engine.getKieSession();
ksession.startProcess("com.sample.bpmn.hello");
manager.disposeRuntimeEngine(engine);
System.exit(0);
}
private static RuntimeManager createRuntimeManager(KieBase kbase) {
JBPMHelper.startH2Server();
JBPMHelper.setupDataSource();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
RuntimeEnvironmentBuilder builder = RuntimeEnvironmentBuilder.Factory.get()
.newDefaultBuilder().entityManagerFactory(emf)
.knowledgeBase(kbase);
return RuntimeManagerFactory.Factory.get()
.newSingletonRuntimeManager(builder.get(), "com.sample:example:1.0");
}
}
Then, this is error in console window:
Exception in thread "main" java.lang.IllegalArgumentException: Driver class name cannot be empty.
at org.kie.test.util.db.internal.DatabaseProvider.fromDriverClassName(DatabaseProvider.java:32)
at org.kie.test.util.db.DataSourceFactory.setupPoolingDataSource(DataSourceFactory.java:57)
at org.kie.test.util.db.DataSourceFactory.setupPoolingDataSource(DataSourceFactory.java:42)
at org.jbpm.test.JBPMHelper.setupDataSource(JBPMHelper.java:103)
at com.sample.ProcessMain.createRuntimeManager(ProcessMain.java:34)
at com.sample.ProcessMain.main(ProcessMain.java:23)
jBPMHelper no longer sets default values for H2,- https://github.com/kiegroup/drools/commit/34293e9675ae4f36f2a3a9e633305bbcc8260d19
We need to use - PersistenceUtil.setupPoolingDataSource(); instead JBPMHelper.setupDataSource();
Also include datasource.properties file at resources folder.
datasource.properties - > https://github.com/kiegroup/jbpm/blob/master/jbpm-examples/src/main/resources/datasource.properties
I face strange execution behaviors in login test methods. I run this code Under selenium Grid. and Grid is configured as a standalone server. So, first I start the selenium grid(Hub\Node) using the batch file to execute by tests.
Following is my class and specs.
code:
1. pojDataSource.java:
public class pojDataSource {
private static WebElement element = null;
private static List<WebElement> elements = null;
public static WebElement txt_UserName(WebDriver driver){
driver.findElement(By.id("txtUserName")).clear();
element = driver.findElement(By.id("txtUserName"));
return element;
}
public static WebElement txt_Password(WebDriver driver){
driver.findElement(By.id("txtPassword")).clear();
element = driver.findElement(By.id("txtPassword"));
return element;
}
}
clsConstant.java:
public class clsConstant {
public static final String URL = "http://localhost:1234/";
public static final String Username = "username";
public static final String Password = "password";
}
ModuleTest.java:
public class ModuleTest {
public RemoteWebDriver mDriver = null;
public DesiredCapabilities mCapability = new DesiredCapabilities() ;
public WebElement mWebElement = null;
public String mBaseURL = clsConstant.URL;
public static clsExcelSampleData mAddConnectorXls;
#Test
public void beforeMethod() throws Exception {
WebDriverWait wdw =null;
mCapability.setCapability("platform", org.openqa.selenium.Platform.WINDOWS);
mCapability = DesiredCapabilities.firefox();
mCapability.setVersion("45.0.2");
mDriver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub/"), mCapability);
mDriver.get(mBaseURL);
mDriver.manage().window().maximize();
pojDataSource.txt_UserName(mDriver).sendKeys(clsConstant.Username ) ;
pojDataSource.txt_Password(mDriver).sendKeys(clsConstant.Password ) ;
pojDataSource.btn_LogIn(mDriver).click();
}
When I execute the code in DEBUG mode in eclipese IDE it shows me the strange behaviors. First it start browser and open the mBaseURL successful with login screen. After loading page it shows default userName\password in browser.
Now when debug point comes to pojDataSource.txt_UserName(mDriver).sendKeys(clsConstant.Username ); line. By pressing F5 my debug point goes to pojDataSource.txt_Password(); line and it fetch wrong password and script execution fails. I worry about how this will be happens if my debug point is at username but still it goes to fetch value of password?
Tried solutions:
1. As I use Firefox browser to run test. I clear my password from browser catch.
Recheck the WebElements IDs and make sure they are reachable by WebDriver while debugging. Also try to avoid using 'static' to WebElements. Take a look on Page Objects Pattern.
Is it possible to run cross browser testing from TestNG with selected test cases on selected browser? For example. 1 - 5 test cases on IE and 6-10 on chrome etc.
Thanks,
Sudhakar
Yes, this is possible
Let me show you how i have done this
Into TestNG suite file i will give browser name as parameter
Now add the code to fetch browser name in beforeTest method
#BeforeTest(alwaysRun = true)
public void fetchSuiteConfiguration(ITestContext testContext) {
targetBrowser=testContext.getCurrentXmlTest().getParameter("selenium.browser");}
Now initialize browser into beforeMethod
#BeforeMethod(alwaysRun = true)
public void setUp(Method method, ITestContext testContext) {
if (targetBrowser == null || targetBrowser.contains("firefox")) {
/*initialize firefox driver here*/
} else if (targetBrowser.contains("ie")) {
/*initialize ie driver here*/
} else if (targetBrowser.contains("opera")) {
/*initialize opera driver here*/
} else if (targetBrowser.contains("chrome")) {
/*initialize Chrome driver here*/
}
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(url);
driver.manage().window().maximize();
}
is there any ways to achieve type casting of local instance of WebDriver through ThreadLocal<WebDriver> to MarionetteDriver??? My code goes like this
public class Base_Class
{
protected ThreadLocal<WebDriver> Driver = null;
#BeforeMethod
#Parameters("BrowserName")
public void setUp(#Optional("Firefox") String BrowserName) throws MalformedURLException
{
Driver = new ThreadLocal<WebDriver>();
if(BrowserName.equalsIgnoreCase("FireFox"))
{
System.setProperty("webdriver.gecko.driver", "..//BrowserDrivers//wires");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
Driver = new <ThreadLocal<WebDriver>> MarionetteDriver(capabilities);
}
else if(BrowserName.equalsIgnoreCase("Chrome"))
{
System.setProperty("webdriver.chrome.driver", "..//BrowserDrivers//chromedriver");
Driver = new <ThreadLocal<WebDriver>>ChromeDriver();
}
}
public WebDriver getDriver()
{
return Driver.get();
}
#AfterMethod
public void closeBrowser()
{
getDriver().quit();
}
}
And all the test case are defined in separate classes which extends this above Base_Class.
Getting Error # Driver = new <ThreadLocal<WebDriver>> MarionetteDriver(capabilities); and Driver = new <ThreadLocal<WebDriver>>ChromeDriver(); lines as Type mismatch: cannot convert from MarionetteDriver to ThreadLocal<WebDriver>
I am using Chrome Version 52.0.2743.116 (64-bit) and FireFox Version 48.0 version browsers on Ubuntu 14.04 Os and Selenium version selenium-server-standalone-2.53.0
Wanted to achieve parallel test execution through testng.xml file..
any help would be highly appreciated..
You need to make the below changes in your code to make it compile.
Create MarionetteDriver object with your DesiredCapabilities and
Set this Driver object inside ThreadLocal object using its set method.
Like below :
if(BrowserName.equalsIgnoreCase("FireFox")) {
System.setProperty("webdriver.gecko.driver", "..//BrowserDrivers//wires");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
// Commented out below line from your code
//Driver = new <ThreadLocal<WebDriver>> MarionetteDriver(capabilities);
Driver.set(new MarionetteDriver(capabilities));
}
Try this and let me know