Three browser windows are launched but all Login details are entered in single browser window. In the other two browser windows login data are not entered and the test gets failed.
It occurs for thread count greater than one.
Following are the Debugging approaches
- Extracted Jar file works well with Jmeter if the thread count is 1.
- Code works fine when launched from IDE
IDE Used: Eclipse
Project Build: Using Maven Plugin
My Test Class (Main Class)
package test.myproject.com;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import com.myproject.baseclass.TestBase;
import com.myproject.elements.HomePage;
import com.myproject.elements.OrganisationPage;
import com.myproject.elements.SideMenu;
import com.myproject.util.Helper;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestLoginPage extends TestBase {
HomePage homepage;
SideMenu sidemenu;
OrganisationPage organisationpage;
public TestLoginPage(){
super();
}
@Before
public void BrowserSetup() {
intialization();
}
//@Test
public void MakeLogin() throws InterruptedException {
homepage=new HomePage();
System.out.println(homepage.GetBuildVersion());
Thread.sleep(2000);
homepage.EnterEmail("[email protected]");
homepage.EnterPassword("*****");
homepage.PerformLogin();
System.out.println(driver.getTitle());
}
@Test
public void CreateProject() throws InterruptedException {
MakeLogin();
sidemenu=new SideMenu();
sidemenu.administartion_click();
sidemenu.organisation_click();
organisationpage=new OrganisationPage();
organisationpage.org_setting_click();
sidemenu.org_user_click();
organisationpage.create_button_click();
organisationpage.enter_fullname("loadautomationtest"+Helper.generate_string());
organisationpage.enter_username("loadautomationtest"+Helper.generate_string());
organisationpage.enter_emailid("loadautomationtest"+Helper.generate_string()+"@gmail.com");
organisationpage.click_create();
}
@After
public void TearDown() {
driver.close();
}
public static void main(String[] args) {
//JUnitCore junit = new JUnitCore();
//junit.addListener(new TextListener(System.out));
//Result result = junit.run(TestLoginPage.class);
JUnitCore.main("test.myproject.com.TestLoginPage");
}
}
Class will be declared in Testbase.java class
package com.myproject.baseclass;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestBase {
public Properties prop;
public FileInputStream file;
public static WebDriver driver;
public static WebDriverWait wait;
public TestBase() {
try {
prop = new Properties();
file = new FileInputStream("Z:/myproject/assets/property/myproject.properties");
prop.load(file);
//data=new ArrayList<String>();
} catch (Exception e) {
e.printStackTrace();
}
}
public void intialization() {
if (prop.getProperty("browser").contentEquals("chrome")) {
System.setProperty("webdriver.chrome.driver",new File("Z:/myproject/assets/driver/chromedriver.exe").getAbsolutePath());
driver = new ChromeDriver();
} else {
System.out.println("No browser found");
}
try {
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
} catch (Exception e) {
e.printStackTrace();
System.out.println("Alert Site seems very slow!!!! Please increase the page load time out in util class");
driver.close();
System.exit(0);
}
}
}
-
blazemeter.com/blog/mixing-selenium-into-your-load-scenario i don't think this is possible , most documents says that the selelnium scripts execute in only one browser at a timePDHide– PDHide2020年02月27日 13:56:52 +00:00Commented Feb 27, 2020 at 13:56
-
@PDHide but when i run selenium in Jmeter using Webdriver sampler plugin it works fine only if i run from Junit issue occursMohamed Sulaimaan Sheriff– Mohamed Sulaimaan Sheriff2020年02月28日 04:44:28 +00:00Commented Feb 28, 2020 at 4:44
-
Can you put the codePDHide– PDHide2020年02月28日 06:10:09 +00:00Commented Feb 28, 2020 at 6:10
-
@PDHide code will not work as the application hosted in our local server i will make a sample script of any other website and send you the codeMohamed Sulaimaan Sheriff– Mohamed Sulaimaan Sheriff2020年02月28日 06:16:12 +00:00Commented Feb 28, 2020 at 6:16
-
I meant the code showing how you call jmeter from junitPDHide– PDHide2020年02月28日 06:36:41 +00:00Commented Feb 28, 2020 at 6:36
1 Answer 1
It's hard to say what's wrong without seeing your intialization();
function, most probably you're facing some form of a race condition
As per WebDriver FAQ:
Q: Is WebDriver thread-safe?
A: WebDriver is not thread-safe. Having said that, if you can serialise access to the underlying driver instance, you can share a reference in more than one thread. This is not advisable. You /can/ on the other hand instantiate one WebDriver instance for each thread.
Most probably in your case 3 threads are trying to work with a single WebDriver instance (for example you declared it as static)
If you are not feeling too comfortable with multi-threading in Java I would recommend considering switching to WebDriver Sampler plugin or at least check its source code to learn how to properly initialize WebDriver instances so they would be independent and thread-safe
-
I have updated my answer with my base class source code(class which has initialization method)Mohamed Sulaimaan Sheriff– Mohamed Sulaimaan Sheriff2020年03月02日 11:41:56 +00:00Commented Mar 2, 2020 at 11:41
-
I was telepathic enough to guess that your WebDriver declaration is
static
, you cannot use static fields safely when it comes to multithreading.Dmitri T– Dmitri T2020年03月02日 11:54:57 +00:00Commented Mar 2, 2020 at 11:54 -
thank you the issue is because of the static declaration Now its working for meMohamed Sulaimaan Sheriff– Mohamed Sulaimaan Sheriff2020年03月02日 12:56:39 +00:00Commented Mar 2, 2020 at 12:56