Creating a new product in Magento using Selenium
On my other blog an individual asked me how they might create a product using Magium. As a result of that conversation I created a few new grid abstractions with the intent of using them later on to build out functionality that allows you to programmatically create a product using the Magium API. I haven’t done that yet, but I did use the new abstractions to create an example about how you can create a new simple product using Magium.
The example is a little verbose, but, then again, so is (by necessity) creating a product in Magento. It’s also hard to read. Copy it into your IDE and look at it there.
<?php
use Facebook\WebDriver\WebDriverSelect;
use Magium\Actions\WaitForPageLoaded;
use Magium\Magento\AbstractMagentoTestCase;
use Magium\Magento\Actions\Admin\Login\Login;
use Magium\Magento\Actions\Admin\Widget\ClickActionButton;
use Magium\Magento\Extractors\Admin\Widget\Attribute;
use Magium\Magento\Navigators\Admin\AdminMenu;
use Magium\Magento\Navigators\Admin\Widget\Tab;
use Magium\WebDriver\WebDriver;
class AddProductTest extends AbstractMagentoTestCase
{
public function testCreateProduct()
{
$this->getTheme('Admin\ThemeConfiguration');
$this->getAction(Login::ACTION)->login();
$this->getNavigator(AdminMenu::NAVIGATOR)->navigateTo('Catalog/Manage Products');
$this->byText('Add Product')->click();
$extractor = $this->getExtractor(Attribute::EXTRACTOR);
/* @var $extractor Attribute */
$attributeSet = new WebDriverSelect($extractor->getElementByLabel('Attribute Set'));
$attributeSet->selectByVisibleText('Default');
$attributeSet = new WebDriverSelect($extractor->getElementByLabel('Product Type'));
$attributeSet->selectByVisibleText('Simple Product');
$this->byText('Continue')->click();
// Set the General requirements
$extractor->getElementByLabel('General::Name')->sendKeys('My Product Name');
$extractor->getElementByLabel('General::Description')->sendKeys('My Product Description');
$extractor->getElementByLabel('General::Short Description')->sendKeys('My Product Short Description');
$extractor->getElementByLabel('General::SKU')->sendKeys('my-product-sku');
$extractor->getElementByLabel('General::Weight')->sendKeys('1.0');
$status = new WebDriverSelect($extractor->getElementByLabel('General::Status'));
$status->selectByVisibleText('Enabled');
// Set the prices
$extractor->getElementByLabel('Prices::Price')->sendKeys('19.99');
$status = new WebDriverSelect($extractor->getElementByLabel('Prices::Tax Class'));
$status->selectByVisibleText('Taxable Goods');
// Set the inventory
$extractor->getElementByLabel('Inventory::Qty')->sendKeys(1000);
$stock = new WebDriverSelect($extractor->getElementByLabel('Inventory::Stock Availability'));
$stock->selectByVisibleText('In Stock');
// Add to a website (this might require some customization)
$this->getNavigator(Tab::NAVIGATOR)->navigateTo('Websites::Product In Websites');
$this->byCssSelector('.website-checkbox')->click();
// Add it to a category
$this->getNavigator(Tab::NAVIGATOR)->navigateTo('Categories::Product Categories');
// This will select the first category. More succinct Xpath will likely be required
$this->byXpath('//input[@class="l-tcb"]')->click();
$element = $this->getAction(ClickActionButton::ACTION);
$element->click('Save');
$this->getAction(WaitForPageLoaded::ACTION)->execute($this->webdriver->byXpath('//body'));
$this->assertElementDisplayed($this->getTheme('Admin\ThemeConfiguration')->getSuccessfulActionXpath(), WebDriver::BY_XPATH);
}
}