Accessing the path to the test case in Selenium IDE - selenium-ide

Is it possible to evaluate the path to the current test case in Selenium IDE? File uploads require a full path, and hard coding one breaks portability.

A good workaround would be to add a test file to the project's resources directory. Every member of the team would have access to the file.
WebElement fileInput = driver.findElement(By.locator("yourButton"));
String filePath = YourClass.class.getResource("/" + fileName).getPath();
fileInput.sendKeys(filePath);
I have found that putting the above code block in a helper method that can be accessed by all test classes works best.

Related

How can I change the Flutter test file search pattern from "_test"

I'd like flutter_test to find test files without the "_test" suffix. Where can I configure this pattern?
For python with pytest for instance, you'd change this setting in pytest.ini:
testpaths =
tests
python_files = *.py
What's the Dart way of doing this?
I have gone through your question in-depth and I found these things.
I checked the test package and dig deep into the source code to see how they were actually doing the checking for _test.dart files. And I found out that in the pubspec.yaml, they have one dependency called glob (link) which I think they used to filter the files. I went through their code and found these particular lines for it:
Link to this page
I tried to fork the repository and then change the type there but it was still showing the same test files as before. So I tried a different approach.
I tried to look into the VS Code plugin for test to see if I can change the type there but I couldn't found the exact module in which there defining the path. In VS-Code, we have an option in settings.json to search the test files outside of the test folder by this line.
"dart.allowTestsOutsideTestFolder": true
But there weren't any concrete options to change the test file search pattern for it. So my conclusion is if we were able to change the search pattern then we have to change in so many places which could also break some things. Therefore I would suggest to stick to the convention of it.
Within Flutter there is no convenient option, you can specify which test files to execute. But this will result in an very heavy test script.
Such as:
flutter test test/file1.dart test/file2.dart ...
EDIT:
Based on the answer of Cavin Macwan. You can create an file in the root named dart_test.yaml with the following content:
filename: "*.dart"
Note: This only works with dart test and not flutter test

sbt how to access base directory of project in scala code

I have been given a code which was created by a vendor and seems like their engineer did a lot of hardcoding in the unit tests.
I have a unit test for a function which outputs the full absolute path of report generated as part of the code as a string.
currently the unit test/assertion that fails looks like
val reportPath = obj.getReportPath()
assert(reportPath.equals("file:/Users/khalid.mahmood/ReportingModule/target/report.csv")
where ReportingModule is the name of the project.
The code logic is fine as for me the value of the reportPath variable comes out to be:
file:/Users/vikas.saxena/coding_dir/ReportingModule/target/report.csv
Since I have the project cloned in a subdirectory called coding_dir in my home directory so the logic looks fine to me.
I want to modify the assertion to ensure that the code pics up the base directory of project by itself and on googling I found that sbt has base as the equivalent of project.baseDir (from maven) from this link
However the following code changes haven't worked out for me
assert(reportPath.equals(s"""$base""" + "/target/report.csv")
Can I get some pointers on how to get this right.
If you're using ScalaTest, you can the ConfigMap to do it.
First you need to tell the ScalaTest Runner to add the path to the ConfigMap. This can be done in your .sbt file like so:
Test / testOptions += Tests.Argument(
TestFrameworks.ScalaTest, s"-DmyParameter=${baseDirectory.value}")
(note that it doesn't have to be baseDirectory.value, many other sbt settings will work. I would suggest target.value for your specific use case).
In the test itself, you then need to access the value from the ConfigMap. The easiest way to do this is to use a Fixture Suite (such as FixtureAnyFunSuite) and mix in the ConfigMapFixture trait:
import org.scalatest.funsuite.FixtureAnyFunSuite
import org.scalatest.fixture.ConfigMapFixture
class ExampleTest extends FixtureAnyFunSuite with ConfigMapFixture {
test("example") { configMap =>
val myParameter = configMap.getRequired[String]("myParameter")
// actual test logic goes here
succeed
}
}
There are of course other ways to solve the problem. For instance, you can also simply get the current working directory (cwd) and work from there. However the downside to that is that in multi-module builds, the cwd will be different depending on whether the Test / fork setting in sbt is true or false. So to make your code robust against these sorts of eventualities, I recommend sticking with the ConfigMap way.

Issues with reading xml file after creating jar

We are building an application using ScalaFX. When I run the project in IntelliJIDEA, everything works fine. However, when I create jar file and try to execute it, I am getting errors in reading some xml file.
I tried various solutions posted in SO, but with no use.
package com.app.adt
import scalafx.application.JFXApp
import scalafx.Includes._
import scalafx.scene.Scene
import scala.reflect.runtime.universe.typeOf
import scalafxml.core.{FXMLView, DependenciesByType}
object App extends JFXApp {
val root = FXMLView(getClass.getResource("/com/app/adt/Home.fxml"),
new DependenciesByType(Map(
typeOf[TestDependency] -> new TestDependency("ADT"))))
stage = new JFXApp.PrimaryStage() {
title = "ADT"
scene = new Scene(root)
}
}
The xml file(Home.fxml) is placed in com/app/adt package. I am creating the jar file using sbt-one-jar.
I have tried different combinations of path, but alwasys gives the same error.
Error Stack:
Caused by: javafx.fxml.LoadException:
file:/adt-app_2.11-1.3-SNAPSHOT-one-jar.jar!/main/adt-app_2.11-1.3-S
NAPSHOT.jar!/com/app/adt/Home.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2611)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2589)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at scalafxml.core.FXMLView$.apply(FXMLView.scala:17)
Jar Structure:
adt-app_2.11-1.3-SNAPSHOT-one-jar.jar
|
main
|
adt-app_2.11-1.3-SNAPSHOT.jar
|
com\app\adt
|
App.scala
Home.fxml
Also, I have tried with sbt-assembly instead of sbt-one-jar. But , still getting the same error. :(
Tried with below answers in SO:
Q1
Q2
The real problem is rather tricky. Firstly, one needs to realize that JAR is an archive (e.g. similar to ZIP) and archives are regular files. Thus the archive itself is located somewhere in the file system, hence, it is accessible via URL.
On the contrary, the "subfiles" (entries) are just data-block within the archive. Neither the operating system nor the JVM knows that this particular file is an archive therefore they treat is as a regular file.
If you're interested in deeper archive handling, try to figure out how ZipFile works. JAR is basically ZIP so you're able to apply this class to it.
Java provides Class.getResourceAsStream methods that enables the programmer to read files as streams. This solution is obviously useless in this particular example since the ScalaFX method expects the File instead.
So basically you have three options
Use the stream API in order to duplicate the XML into temporary file, than pass this file to the method.
Deploy your resources separately in a way they remain regular files.
Re-implement JavaFX in order to accept streams (this should probably happen anyway)

Where do I put my resources in Scala?

While studying using Scala with JavaFX I have met the following code in a ProScalaFX example:
val resource = getClass.getResource("AdoptionForm.fxml")
if (resource == null) {
throw new IOException("Cannot load resource: AdoptionForm.fxml")
}
...
val root: jfxs.Parent = jfxf.FXMLLoader.load(resource)
Where do I put the actual "AdoptionForm.fxml" content in this case? Unfortunately I am neither familiar with using resources in Java.
I use SBT as the building system and Idea as an IDE.
There is a related question which suggests a way (putting the resource files in "src/main/resources" or "src/main/resources/packagename"), but it also says it doesn't work actually (needless to say I have tried).
src/main/resources is the correct location for placing resources in a default SBT configuration.
However, one has to be aware of the difference between getClass.getResource and ClassLoader.getResource. Using getClass.getResource("AdoptionForm.fxml") requires the file to be located in a path which corresponds to the package of the class.
For instance: If the class is located in com.domain.utils then the resource must be located at src/main/resources/com/domain/utils/AdoptionForm.fxml.
In order to switch from package-relative locations to absolute locations one can either use ClassLoader.getResource or just prepend the resource string with a /.
Example: getClass.getResource("/AdoptionForm.fxml") loads the resource from src/main/resources/AdoptionForm.fxml

What is the correct way to embed a resource in a reusable MFC class?

I am writing a C++ (MFC in particular) class which uses an external .gif image file and produces another image file after some processing. It would be nice if the initial image could be embedded in the code somehow. I have read in MSDN about using multiple .rc files and the whole thing seems quite complicated.
I would like to know from people who have gone through this before how to handle this problem.
EDIT : Sorry I was not clear. The class I am writing should be standalone, so I could use it again. If I put the image in a resource file, then the class will not compile if used in a fresh project.
You cannot embedd MFC resources inside a class or similar C++ container. They can only be embedded in DLL or EXE files - in a separate section of the produced binary. Since you want your class to be reusable, you must put it in a DLL. Hence, you must tag your class with the AFX_EXT_CLASS keyword.
There are two solutions.
Solution #1:
Create an MFC DLL project (MFC Extension DLL). Call it MyLibrary or whatever.
Put all your standalone classes in this DLL.
Embed all necessary resources.
Let your classes load resources from the HINSTANCE of your DLL as described below.
There are several ways to retrieve the HINSTANCE of your DLL. If you ask me, the best solution is to grab it in DllMain. This is done automatically if you choose the MFC Extension DLL configuration:
static AFX_EXTENSION_MODULE MyLibDLL = { NULL, NULL }; // Make this variable global!
// Then access the hInstance as follows:
LoadResource(MyLibDLL.hModule, ...)
Solution #2:
Store your resource as a byte buffer. Or better, convert it to Base64 and store it as an ASCII string. But remember not to blow the stack! Keep your resources small or increase the stack size in your project settings. Example:
const char *encodedResource = "SGVsbG8gd29ybGQh";
char *data = decode(encodedResource);
foo(data);
In the solution explorer go to resource view, Right click and click Add Resource then click Import and add the gif file. Now you can use your Resource ID to access the gif file in your code.
Just adding the file to a resource doesn't embed the file in the actual resource file it just links to the file. If you open your .rc file you'll see it says something like:
IDB_GIF_MYIMAGE GIF "artwork\\mygif.gif"
During the compilation face the resource will be included in the EXE, which you reference using the resource id IDB_GIF_MYIMAGE. You can reference the same file in other projects without having to duplicate the file.
To embed an image (or any other type of binary data) into your class without using resource files, use the bin2c utility, for example you can download it from here: http://www.opensource.apple.com/source/libpcap/libpcap-16/libpcap/msdos/bin2c.c . Running this on a file will produce what is basically a static array with the bytes of the file as members of that array. Stuff this array into a .h file (or put it in the header of your class, or make it a static member...) and then you will have that file available in-memory without having to use LoadResource() and its brethren.
If you want to use this with CImage::Load(), you will have to write your own class that derives from IStream, and implement a few of the methods in a way so that they 'read' from memory. I don't know of any ways to let CImage decode an image from an in-memory representation of a gif file.
I think the best solution is just to document that to use the class you must also import to your project a certain .gif file and give it a certain expected identifier (e.g. IDB_MYCLASS_MYGIF). You can then use the preprocessor to detect if the resource has been correctly added, e.g.:
#ifndef IDB_MYCLASS_MYGIF
#error Make sure you import mygif.gif to the project. See docs for more info.
#endif
This will prevent the class compiling until the user imports the image properly. Alternatively you could just use #ifdefs to fall back to code which does not use the default image if it is not provided.
Have a look at the CRuntimeDialog class presented in http://www.codeproject.com/Articles/5371/ToDoList-6-5-4-Feature-Release-An-effective-and-fl . It provides a way to create a dialog from the string that makes up the resource definition.