How to generate test report using selenium web driver - selenium

I want to know is any other option available to generate test report except TestNG framework in selenium webdriver

You can try ExtentReports: http://extentreports.com. An example:
public class Main {
public static void main(String[] args) {
// start reporters
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extent.html");
// create ExtentReports and attach reporter(s)
ExtentReports extent = new ExtentReports();
extent.attachReporter(htmlReporter);
// creates a toggle for the given test, adds all log events under it
ExtentTest test = extent.createTest("MyFirstTest", "Sample description");
// log(Status, details)
test.log(Status.INFO, "This step shows usage of log(status, details)");
// info(details)
test.info("This step shows usage of info(details)");
// log with snapshot
test.fail("details", MediaEntityBuilder.createScreenCaptureFromPath("screenshot.png").build());
// test with snapshot
test.addScreenCaptureFromPath("screenshot.png");
// calling flush writes everything to the log file
extent.flush();
}
}

try Allure reporting framework
https://github.com/allure-framework
Hope this helps

Related

How to make an executable jar file using IntelliJ from a Selenium/Junit java file?

I am trying to generate a jar file of my test to use it in JMeter, I am trying this solution How to make an executable jar file using IntelliJ from a Selenium/TestNG java file? but the problem in on step 4.
4- Select your main class using the browse button.
When I search for class I can't select mine .java code, how I can do the step 4 possible?
My code
public class Jmeter {
WebDriver driver = null;
#Test
public void testLoad() {
// Robot robot = new Robot();
ChromeOptions options = new ChromeOptions();
options.addArguments(Arrays.asList("disable-infobars", "ignore-certificate-errors", "start-maximized","use-fake-ui-for-media-stream"));//
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("myurl");
my commands
}
}
Does someone know how to solve this?
Solution
I created a class with the main method to call my test.
package Jmeter;
public class JmeterMain {
public static void main(String[] args) {
Jmeter test = new Jmeter();
test.testLoad();
}
}

Unable to download testng from http://beust.com/eclipse/ due to company policy .

I have created a maven project , with cucumber BDD and testNG . However to use testng i need to install the testng pluggin from eclipse help . The problem is my company has blocked usage of such external connections . Is there an alternative for this .
Following are your options.
See if you can get an approval from your company's IT department to whitelist the eclipse plugin download site so that you can install it via eclipse (or) have them download the eclipse plugin jar separately, and you can drop the jar in the dropins folder so that eclipse is aware of it. For more information, please refer to the answers of this stackoverflow question.
If available, resort to using an alternative IDE such as IntelliJ. IntelliJ unlike eclipse, comes pre-installed with the TestNG plugin and should suffice.
You leverage the build tool such as Maven/Ant/Gradle to run your tests from the command prompt. Both Maven and Gradle lets you run even 1 single test also at a given time. So you should be able to easily run tests without the IDE, from the command prompt (which is eventually how your tests would be run in a Continuous Integration environment such as Jenkins)
You create a main() method housing class, which would use the TestNG APIs directly to create tests. So every-time you want to run a TestNG test class or a suite etc., you would merely go back to your runner class, update it with the details and then run via it [ To me this option should be your last resort]
Here's a full fledged sample for option (4), which you should be able to start tweaking for your own use.
public class Practice {
public static void main(String[] args) {
for (String each : new String[]{"A", "B"}) {
runWith(each);
}
}
private static void runWith(String group) {
TestNG testNG = new TestNG();
XmlSuite xmlSuite = new XmlSuite();
xmlSuite.setName("suite");
XmlTest xmlTest = new XmlTest(xmlSuite);
xmlTest.setName("test");
xmlTest.addIncludedGroup(group);
XmlClass clazz = new XmlClass(Practice.class);
clazz.loadClasses();
xmlTest.getClasses().add(clazz);
testNG.setXmlSuites(Collections.singletonList(xmlSuite));
System.out.println(xmlSuite.toXml());
testNG.run();
}
#Test(dataProvider = "SearchProvider", groups = "A")
public void testMethodA(String author, String searchKey) {
System.out.println("testMethodA :" + author + ", " + searchKey);
}
#Test(dataProvider = "SearchProvider", groups = "B")
public void testMethodB(String searchKey) {
System.out.println("testMethodB :" + searchKey);
}
#DataProvider(name = "SearchProvider")
public Object[][] getDataFromDataprovider(ITestContext c) {
Object[][] groupArray = null;
for (String group : c.getIncludedGroups()) {
if (group.equalsIgnoreCase("A")) {
groupArray = new Object[][]{
{"Guru99", "India"},
{"Krishna", "UK"},
{"Bhupesh", "USA"}
};
break;
} else if (group.equalsIgnoreCase("B")) {
groupArray = new Object[][]{
{"Canada"},
{"Russia"},
{"Japan"}
};
}
break;
}
//return groupArray;
return groupArray;
}
}

Can't run my test JUnit step definition

I want to run test JUnit i work in 2 different projets :
In the first one i have
The feature :
Feature: Google Search
Scenario: Validate google search text field
Given open a browser
When navigate to google page
Then validate search text field
The Step definition Class :
#Given("^open a browser$")
public void open_a_browser() throws Throwable {
System.setProperty("webdriver.chrome.driver", "D:\\Drive\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
#When("^navigate to google page$")
public void navigate_to_google_page() throws Throwable {
driver.get("http://www.google.com");
}
#Then("^validate search text field$")
public void validate_search_text_field() throws Throwable {
driver.findElement(By.name("q")).sendKeys("selenium "); }
And the Runner class :
#RunWith(Cucumber.class)
#CucumberOptions(features={"src\\test\\resources\\com\\tutoriel\\testing\\features\\Googlesearch.feature",
glue={"com\\tutoriel\\testing\\stepdefinitions"})
public class RunTestRunner {
}
And it works for me very good
but when i tr to add those 3steps (feature, stepdef and the runner ) in an big another project it doesn't work
i don't undersand the issue !!
this is the error
You can implement missing steps with the snippets below:
but i have already defined them in the class steps definition
Anyone can help me please

How do I replace one test in an XML-defined suite with three others in TestNG?

Our team uses TestNG to run some tests in Selenium. We need to be able to run a given test on 3 different browsers (Chrome, Firefox, and [sadly] IE). We have a browser parameter on our base test class and really we could just declare three tests, one each for each browser; however, we'd really like to just be able to specify the browser value as "Standard 3" and have that run the test on each browser automatically.
So, I've built a class that implements ISuiteListener and attempts to create the new tests on the fly. However, any way I try to add tests fails. That is, no new tests I try to add will be executed by the suite. It's like nothing I did actually changed anything.
Here's my code:
public class Standard3BrowserSuiteListener implements ISuiteListener {
#Override
public void onStart(final ISuite suite) {
final XmlSuite xmlSuite = suite.getXmlSuite();
final Map<String, String> suiteParameters = xmlSuite.getParameters();
final List<XmlTest> currentTests = new ArrayList<XmlTest>(xmlSuite.getTests());
final ArrayList<XmlTest> testsToRun = new ArrayList<XmlTest>(currentTests.size());
for (final XmlTest test : currentTests) {
final Browser browser;
final Map<String, String> testParameters = test.getAllParameters();
{
String browserParameter = testParameters.get("browser");
if (browserParameter == null) {
browserParameter = suiteParameters.get("browser");
}
browser = Util.Enums.getEnumValueByName(browserParameter, Browser.class);
}
if (browser == Browser.STANDARD_3) {
XmlTest nextTest = cloneTestAndSetNameAndBrowser(xmlSuite, test, testParameters, "Chrome");
xmlSuite.addTest(nextTest);
testsToRun.add(nextTest); // alternate I've tried to no avail
nextTest = cloneTestAndSetNameAndBrowser(xmlSuite, test, testParameters, "Firefox");
xmlSuite.addTest(nextTest);
testsToRun.add(nextTest); // alternate I've tried to no avail
nextTest = cloneTestAndSetNameAndBrowser(xmlSuite, test, testParameters, "IE");
xmlSuite.addTest(nextTest);
testsToRun.add(nextTest); // alternate I've tried to no avail
} else {
testsToRun.add(test);
}
}
// alternate to xmlSuite.addTest I've tried to no avail
testsToRun.trimToSize();
currentTests = xmlSuite.getTests();
currentTests.clear();
currentTests.addAll(testsToRun);
}
private XmlTest cloneTestAndSetNameAndBrowser(final XmlSuite xmlSuite, final XmlTest test,
final Map<String, String> testParameters, final String browserName) {
final XmlTest nextTest = (XmlTest) test.clone();
final Map<String, String> nextParameters = new TreeMap<String, String>(testParameters);
nextParameters.put("browser", browserName.toUpperCase());
nextTest.setName(browserName);
final List<XmlClass> testClasses = new ArrayList<XmlClass>(test.getClasses());
nextTest.setClasses(testClasses);
return nextTest;
}
#Override
public void onFinish(final ISuite suite) {}
}
How can I replace the test with the browser value "Standard 3" with 3 tests and have it run properly? Thanks!
Here's what you need to do :
Upgrade to the latest released version of TestNG.
Build an implementation of org.testng.IAlterSuiteListener
Move your implementation that you created in ISuiteListener into this listener implementation.
Wire in this listener via the <listeners> tag in your suite XML File (or) via ServiceLoaders (As described in the javadocs of this interface)

Setting separate profiles for Parallel Selenium Tests

I am researching how to set an individual profile using RemoteWebDriver. I have been reading about it on the following thread.
http://stackoverflow.com/questions/12961037/parallel-execution-of-firefoxdriver-tests-with-profile-share-same-profile-copy
I am trying to tackle it as following:
public static RemoteWebDriver getDriver(String methodName) throws MalformedURLException {
String SELENIUM_HUB_URL = "http://localhost:4444/wd/hub";
ThreadLocal<RemoteWebDriver> remoteWebDriver = null;
File currentProfileFile = new File(methodName);
//This is where it gives the error
FirefoxProfile currentFireFoxProfile = new FirefoxProfile(currentProfileFile);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(FirefoxDriver.PROFILE, currentFireFoxProfile);
String proxy = System.getProperty("proxy");
try {
remoteWebDriver = new ThreadLocal<RemoteWebDriver>();
remoteWebDriver.set(new RemoteWebDriver(new URL(SELENIUM_HUB_URL),
capabilities));
} catch (MalformedURLException e) {
System.out.println("Please fix the RemoteDriverSetup.class");
}
remoteWebDriver.get().manage().window()
.setSize(new Dimension(2880, 1524));
remoteWebDriver.get().manage().timeouts()
.pageLoadTimeout(10, TimeUnit.SECONDS);
remoteWebDriver.get().manage().timeouts()
.implicitlyWait(10, TimeUnit.SECONDS);
return remoteWebDriver.get(); // Will return a thread-safe instance of the WebDriver
}
I am getting the following error :
Time elapsed: 1.044 sec <<< FAILURE!
org.openqa.selenium.firefox.UnableToCreateProfileException: Given model profile directory does
not exist: TEST001
Update : I am injecting method name in the BaseTest class below
#BeforeMethod
public void startTest(Method testMethod) {
LOG.info("Starting test: " + testMethod.getName());
this.driver = WebDriverSetup.getDriver(testMethod.getName());
}
If you don't want to customize anything on your Firefox profile, better to create Firefox webdriver instance by NOT providing any profile details (as mentioned by Nguyen).
If you really want to create separate profiles (may be required to install some plug-ins like Firebug), in that case, you can do that by without passing any file name as below:
FirefoxProfile currentFireFoxProfile = new FirefoxProfile();
//Do some customization - add extension
currentFireFoxProfile.addExtension(pathOfextensionToInstall);
//or Setup some Firefox config. switch values
currentFireFoxProfile.setPreference("browser.download.manager.showWhenStarting", false);