how to add the addon adblockplus to firefox profile using webdriver - selenium

i am using below code for adding the extension adblock plus to firefox profile
File f = new File("C:\FirefoxProfile\extensions\{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}.xpi");
fp.addExtension(f);
but when i add this, i am getting error unable to find the location.
can any one please answer this .

Your path is not correct.
Correct path would be: C:\\FirefoxProfile\\extensions\\{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}.xpi
Java reads \\ as \
Check out this simple program:
public class MainClass {
public static void main(String[] a) {
File myDir = new File("C:\\jdk1.5.0\\src\\java\\io");
System.out.println(myDir);
}
}
The output will be: C:\jdk1.5.0\src\java\io
EDIT:
public void aaaa() throws IOException {
File file = new File("C:\\FirefoxProfile\\adblock_plus-2.6.6-tb+an+fx+sm");
FirefoxProfile fp = new FirefoxProfile();
fp.addExtension(file);
}

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();
}
}

Adding Apache POI 4.0.1 library not sufficient to use XSSFWorkbook

I am using the following tutorial to realize a Selenium Keyword Driven Framework : http://toolsqa.com/selenium-webdriver/keyword-driven-framework/set-excel-apache-poi/
For the part which ask to create an "util" package with an ExcelUtils class, I followed the instructions which begin by adding a jar to the project libraries.
This jar is for the library apache-poi-4.0.1 : poi-4.0.1.jar.
But even with this library and it's attached source, classes XSSFWorkbook, XSSFSheet and XSSFCell do not exist.
So my question is, which part of the tutorial I am missing? Or which library I am missing?
I am using Eclipse Oxygen with the JRE JavaSE-1.8
Package utils;
import java.io.FileInputStream;
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
//This method is to set the File path and to open the Excel file
//Pass Excel Path and SheetName as Arguments to this method
public static void setExcelFile(String Path,String SheetName) throws Exception {
FileInputStream ExcelFile = new FileInputStream(Path);
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
}
//This method is to read the test data from the Excel cell
//In this we are passing parameters/arguments as Row Num and Col Num
public static String getCellData(int RowNum, int ColNum) throws Exception{
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
String CellData = Cell.getStringCellValue();
return CellData;
}
}
You are missing the below piece of code
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
I finally found a solution.
I had to download 5 other libraries :
poi-examples-4.0.1
poi-excelant-4.0.1
poi-ooxml-4.0.1
poi-ooxml-schemas-4.0.1
poi-scratchpad-4.0.1
After that, I can use XSSF classes.
You need the poi-ooxml dependency as well.
This is how it looks in Gradle, just change $apachePoiVersion to the version you want.
implementation "org.apache.poi:poi:$apachePoiVersion"
implementation "org.apache.poi:poi-ooxml:$apachePoiVersion"

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

unable to create webdriver object reference

I am trying to create webdriver object reference in the class which is not having main method. it is not allowing me to create the object. Finally, I tried with an instance and it is not working(not identifying driver)
public class LoginPage
{
public static WebDriver driver;
static ResourceLoader loader = new ResourceLoader();
public static String loginVerify(String username, String password)
{
System.out.println("navigated to loginvarify() :: ");
logger.info("username is "+username);
logger.info("password is "+password);
try
{
driver.findElement(By.id(loader.getProperty(Constants.UserName))).sendKeys(username);
}
Please help me
public class LoginPage
{
System.setProperty("webdriver.chrome.driver", "src/main/resources/drivers/osx/chromedriver"); // path to your chrome driver
public static WebDriver driver = new ChromeDriver();
static ResourceLoader loader = new ResourceLoader();
public static String loginVerify(String username, String password)
{
System.out.println("navigated to loginvarify() :: ");
logger.info("username is "+username);
logger.info("password is "+password);
try
{
driver.findElement(By.id(loader.getProperty(Constants.UserName))).sendKeys(username);
}
I have updated your code, kindly refer you are missing your driver initialization. I have initialized to chromeDriver(), if you are using firefox initialize it accordingly.
System.setProperty("webdriver.chrome.driver",
"src/main/resources/drivers/osx/chromedriver"); // path to your chrome
driver
If you are not having the chrome driver, kindly download and place it in your directory path and pass the path reference accordingly.
https://sites.google.com/a/chromium.org/chromedriver/downloads
Let me know if you still face any issue or anything is not clear.
WebDrvier driver = new FireFoxDriver(); //driver initialization
driver.FindElement(By.Id(IdName)); //to find elements by ID in UI
You can define your chrome driver path using System.setProperty("webdriver.chrome.driver", "C:\Spring Workspace\chromedriver\chromedriver.exe"); // path to your chrome driver or you can define the path in the environment variable so you don't have to use this setProperty code again and again......if you are using firefox driver than not need to define any setProperty beacause it is already defined inside org.openqa.selenium.firefox.FirefoxDriver....

jbehave run only specific story

I have jbehave integrated with Selenium. I am running my tests through command line as below
C:\eclipse_workspace\MySeleniumTests>mvn clean test -Dwebdriver.firefox.bin="C:\Program Files\Mozilla\Firefox\firefox.exe"
I have used jbehave-maven-plugin. Maven picks up all the Embedder impl (JunitStories in my case) from the source directory and execute them one by one. Configuration for that is <include>**/*Stories.java</include> in pom.xml
It then looks for relevant .story files in the specified dir and executes them. Say, I have two story files one.story and two.story, both of them are executed.
Over a time, number of story files are going to increase I only want to execute specific story files should there be a way to do this? I am thinking to pass specific story file names as run time parameters but don’t know what is required to make that happen.
I got it working with the below code
mvn clean test -Dwebdriver.firefox.bin="C:\Program Files\Mozilla\Firefox\firefox.exe" -Dstory=myStory.story
Override storyPaths() method in embedder class as below.
public class MyTestStories extends JUnitStories /* InjectableEmbedder */{
#Override
protected List<String> storyPaths() {
List<String> storiesToRun = new ArrayList<String>();
String storyProperty = System.getProperty("story");
if (storyProperty == null || storyProperty.isEmpty()) {
throw new RuntimeException("Please specify which stories to run");
}
String[] storyNames = storyProperty.split(",");
StoryFinder sf = new StoryFinder();
URL baseUrl = CodeLocations.codeLocationFromClass(this.getClass());
for (String storyName : storyNames) {
storiesToRun.addAll(sf.findPaths(baseUrl, storyName, ""));
}
return storiesToRun;
}
Try the following:
mvn clean test -Dwebdriver.firefox.bin="C:\Program Files\Mozilla\Firefox\firefox.exe" -Djbehave.story.name=<story filename without extension (wildcards are supported)>
You should also use custom test suite implementation:
public abstract class JBehaveTestSuite extends ThucydidesJUnitStories {
private static final String STORY_NAME_PATTERN = "**/${jbehave.story.name:*}.story";
public JBehaveTestSuite() {
findStoriesCalled(storyNamesFromEnvironmentVariable());
}
#Override
public void run() throws Throwable {
super.run();
}
private String storyNamesFromEnvironmentVariable() {
return SystemPropertyUtils.resolvePlaceholders(STORY_NAME_PATTERN);
}
}