Example (WordPress)
Below is a sample WPSelenium test for WordPress. As you look through it, do note the use of the wpBeforeRun
annotation, and the wp_get_theme
WordPress-Core function.
use Facebook\WebDriver\WebDriverBy;
use WPSelenium\WPSTestCase;
class WPSeleniumWordPressTest extends WPSTestCase
{
/**
* @wpBeforeRun beforeRunSetTitle
*/
function testTitleChangingPlugin()
{
//loading the WordPress site
$this->driver->Get($this->GetTestSite());
//Using WordPress core `wp_get_theme` function.
$currentTheme = wp_get_theme();
//test pass condition
$this->assertSame($currentTheme->get( 'Name' ) , $this->driver->getTitle());
}
static function beforeRunSetTitle()
{
// Class/plugin that set the title of the Site to the theme
$setThemeAsTitlePlugin = new SetThemeAsTitlePlugin();
$setThemeAsTitlePlugin->Run();
}
}
In the example above we want to test our frivolous SetThemeAsTitle Plugin which is meant to set the title of the site to the theme name on request.
For this we write a test that gets the current theme name (using the WordPress-Core function wp_get_theme
), and asserts that the browser window's title is the same as the obtained theme name.
Running this test without the wpBeforeRun
annotation linked to the beforeRunSetTitle
function would always fail as we will not have instatiated our SetThemeAsTitle Plugin (i.e will default to the persisted title). Adding this will ensure we do so before the tests are run.