I'm writing a Gherkin-based acceptance testing PoC. I have a feature file, step object, and a page object. In my sequence I will need to log-in the test user before conducting the rest of the series. Our SUT is a legacy PHP application that didn't use any framework.
I would like to store the testuser's credentials in a params.yml or other external config file but have been unsuccessful in making this work and unable to find a complete example.
My login object is a simple Cest class for now. I didn't think it needed its own feature description, the rest of the tests will be Gherkin based where needed. My config files are currently the default configs generated by Codeception 5's bootstrap command with a gherkin section added for the one feature file I've written so far. Eventually I will run this under WebDriver to enable sessions...for now I'm just trying to establish a reusable environment we can build on for a team of developers.
The Codeception docs seem to gloss over some of these concepts or recommendations for users new to their framework.
I sincerely appreciate any ideas or concerns you may have.
<?php
namespace Tests\Acceptance;
use Codeception\Attribute\Group;
use Tests\Support\AcceptanceTester;
use Tests\Support\Page\Acceptance\LoginPage;
class LoginCest
{
#[Group('login')]
public function successfulLogin(AcceptanceTester $I, LoginPage $loginPage)
{
$loginPage->login( <testUserHere>, <goodPasshere> ); // <-this is what I want to provide
$I->dontSeeElement('.alert-error');
$I->amOnPage("/command.php");
}
public function unsuccessfulLogin(AcceptanceTester $I, LoginPage $loginPage)
{
$loginPage->login(getenv( <testUserHere> , 'baddpass');
$I->seeElement('.alert-error');
$I->amOnPage("/");
}
}
I ended up writing a config helper:
<?php
declare(strict_types=1);
namespace Tests\Support\Helper;
class Config extends \Codeception\Module
{
protected array $requiredFields = ['testUser', 'goodPass'];
}
Then this in my suite config:
modules:
enabled:
- \Tests\Support\Helper\Config:
testUser: testuser
goodPass: supersecretpassword
It may not be right or a best practice...but it worked so I could move forward.
Now if I can just figure out how to use the helper in my Gherkin-driven StepObject...
Related
Very much an XY problem, but I'm interested in the underlying answer too.
See bottom for XY context.
I'm in a .NET Core 3 AzureFunctions (v3) App project.
This code makes my question fairly clear, I think:
namespace MyProj.Functions
{
internal class CustomStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var isDevelopment = true; //Can I correctly populate this, such that it's true only for local Dev?
if(isDevelopment)
{
// Do stuff I wouldn't want to do in Prod, or on CI...
}
}
}
}
XY Context:
I have set up Swagger/Swashbuckle for my Function, and ideally I want it to auto-open the swagger page when I start the Function, locally.
On an API project this is trivial to do in Project Properties, but a Functions csproj doesn't have the option to start a web page "onDebug"; that whole page of project Properties is greyed out.
The above is the context in which I'm calling builder.AddSwashBuckle(Assembly.GetExecutingAssembly()); and I've added a call to Diagnostics.Process to start a webpage during Startup. This works just fine for me.
I've currently got that behind a [Conditional("DEBUG")] flag, but I'd like it to be more constrained if possible. Definitely open to other solutions, but I haven't been able to find any so ...
While I am not completely sure that it is possible in azure functions I think that setting the ASPNETCORE_ENVIRONMENT application setting as described in https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings should allow you to get whether the environment is set as production or development by injecting a IHostEnvironment dependency and checking
.IsDevelopment()
on the injected dependency.
I have 2 questions:
I was able to develop selenium scripts and then export as jar file and then import in Jmeter.
The whole flow worked but that's for only one request/user. if I wanna run concurrently for multiple requests/users it will return me duplicate/failed.
My question is where and how should I create dynamic variable to avoid that problem?
is Jmeter right tool to record the UI of single page application for performance testing? it seems like not but please answer with enough details.
Load tests needs to be parameterized, i.e. each JMeter Thread (virtual user) should use separate credentials. The most commonly used test element for test parameterization is CSV Data Set Config. You can access JMeter Variables by accessing JMeterContext class like:
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.junit.Test;
public class SomeClass {
public JMeterVariables getJMeterVariables() {
return JMeterContextService.getClientSideVariables();
}
#Test
public void testSomething() {
JMeterVariables vars = getJMeterVariables();
String var1 = vars.get("var1");
String var2 = vars.get("var2");
}
}
you will need to add ApacheJMeter_core to your project CLASSPATH
JMeter can record all HTTP Requests between the browser and the application under test and this is more or less what other load testing tools are doing. However in the majority of cases you won't be able to successfully replay the recorded test without prior correlation
Is there any way that I can compile a gsp view into an html given a provided model from a groovy script without having to run all the Grails application?
The use case is that due to client demands we have to use Javascript/jQuery to create the front-end of the application. We've already had the architecture definition, but we're having issues creating integration front-end tests since our front-end composes of .gsp, javascript and css, all componentized.
For instance: Button may have a .gsp a .js and a .css associated to it.
Ideal solution to create front-end component integration tests: Have the .gsp compiled into html before the tests run so we can run the assertions in the *.test.js files. Since we don't need database, services or other instances to compile the .gsp, no need for the application to be running, avoiding the time to boot up the app.
Thanks in advance!
Next code should help you.
File gspFile = new File(BuildSettings.BASE_DIR, "grails-app/view/path/to/view/${viewName}.gsp")
This code will find gsp, you can find it by absoluteUrl
if(gspFile.exists()) {
def model = model((Class)scaffoldValue)
def viewGenerator = new GStringTemplateEngine()
Template t = viewGenerator.createTemplate(gspFile)
def contents = new FastStringWriter()
t.make(model.asMap()).writeTo(groovy.lang.Writer)
}
Writer you should find by yourself, think that it can be a stream to the file.
Didn't test it, but it should work :)
I'm trying to start writing tests within Laravel.
I think it is good practice to write my own basic TestCase, that runs the test setup (for example migrations).
<?php
class TestCase extends PHPUnit_Framework_TestCase {
public function setUp()
{
parent::setUp();
// just for example:
DB::table('categories')->first();
}
}
After that, I want to inherit all my TestCases from the one created above
<?php
class TestExample extends TestCase {
public function testSomethingIsTrue()
{
$this->assertTrue(true);
}
}
So now I have three problems:
My TestCase throws error, class DB not found. Why the heck is the test not autoloading Laravel Framework Classes?
PHPUnit tells me (with a warning) that my TestCase does not contain any tests, but that is my suspected behaviour for this class, how can I avoid this message?
My TestExample cannot find the class TestCase.
Can you help me understanding that?! How can I configure a test specific autoloading?
UPDATE:
Answer 1: Because I run the tests in NetBeans IDE, that needs to be configured! Setting up the right phpunit.xml helped
As revealed in our discussion, when you run your PHPUnit tests using your IDE (NetBeans, PhpStrom and so), you have to make sure your IDE is correctly configured to catch phpunit.xml.
I have an application made in php for which am using selenium for unit testing using phpUnit. The problem is that I have to set the environment before I can go for tests. For eg. I have to set session variables, login and fetch data from remote server. All this takes a lot of time and it is not feasible to re-set this in every test function.
I am looking for a method so that I can use the same browser session for running all the tests in it. I tried looking for resources online, but couldn't find any good sources for this. The code I have written is
protected function setUp()
{
parent::setUp();
$this->setBrowserUrl("http://localhost/devel/");
}
public function start()
{
parent::start();
$this->open("");
//Setting up the environment here
}
public function testFunction()
{
//A test function
}
public function testFunction2()
{
//Another test function
}
But this is opening browser instance for both the functions. Is there any work around for this? Or is there any command line parameter while launching selenium server for this?
"[I am] using selenium for unit testing using phpUnit"
No, you're not. You're using PHPUnit with selenium for functional testing. :-)
But since it's probably not in your best interest to re-invent that wheel, you want Mink: http://mink.behat.org/
It wraps around Guzzle and lets you do session-based acceptance testing using a bunch of different drivers. It has Goutte for a headless browser, and can work with Selenium and Sahi and a bunch of others.
Also of note, depending on your needs, is Behat: http://behat.org/
It lets you write client-readable test documents that can be turned into Mink-based acceptance tests.
HTH.
Question already answered.
The unaccepted answer did the job for me.
#see How do I run a PHPUnit Selenium test without having a new browser window run for each function?