1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Magento\Actions\Checkout\Steps; |
4
|
|
|
|
5
|
|
|
use Facebook\WebDriver\WebDriverExpectedCondition; |
6
|
|
|
use Magium\Magento\AbstractMagentoTestCase; |
7
|
|
|
use Magium\Magento\Identities\Customer; |
8
|
|
|
use Magium\Magento\Themes\OnePageCheckout\AbstractThemeConfiguration; |
9
|
|
|
use Magium\WebDriver\WebDriver; |
10
|
|
|
|
11
|
|
|
class BillingAddress implements StepInterface |
12
|
|
|
{ |
13
|
|
|
const ACTION = 'Checkout\Steps\BillingAddress'; |
14
|
|
|
|
15
|
|
|
protected $webdriver; |
16
|
|
|
protected $theme; |
17
|
|
|
protected $customerIdentity; |
18
|
|
|
protected $testCase; |
19
|
|
|
protected $shipToDifferentAddress; |
20
|
|
|
|
21
|
|
|
protected $enterNewAddress; |
22
|
|
|
protected $bypass = []; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
WebDriver $webdriver, |
26
|
|
|
AbstractThemeConfiguration $theme, |
27
|
|
|
Customer $customerIdentity, |
28
|
|
|
AbstractMagentoTestCase $testCase |
29
|
|
|
) { |
30
|
|
|
$this->webdriver = $webdriver; |
31
|
|
|
$this->theme = $theme; |
32
|
|
|
$this->customerIdentity = $customerIdentity; |
33
|
|
|
$this->testCase = $testCase; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Allows you to bypass arbitrary element assertions and entry. Currently only supports email address. |
38
|
|
|
* |
39
|
|
|
* @see Magium\Magento\Actions\Checkout\Steps\CustomerBillingAddress |
40
|
|
|
* |
41
|
|
|
* @param $element The name of the element |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
public function bypassElement($element) |
45
|
|
|
{ |
46
|
|
|
$this->bypass[] = $element; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function shipToDifferentAddress($ship = true) |
50
|
|
|
{ |
51
|
|
|
$this->shipToDifferentAddress = $ship; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function enterNewAddress($newAddress = true) |
55
|
|
|
{ |
56
|
|
|
$this->enterNewAddress = $newAddress; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function preExecute() |
60
|
|
|
{ |
61
|
|
|
if ($this->enterNewAddress & $this->webdriver->elementDisplayed($this->theme->getBillingNewAddressXpath(), WebDriver::BY_XPATH)) { |
62
|
|
|
$this->webdriver->byXpath($this->theme->getBillingNewAddressXpath())->click(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->shipToDifferentAddress) { |
66
|
|
View Code Duplication |
if ($this->webdriver->elementExists($this->theme->getDoNotUseBillingAddressForShipping(), WebDriver::BY_XPATH)) { |
|
|
|
|
67
|
|
|
$this->webdriver->byXpath($this->theme->getDoNotUseBillingAddressForShipping())->click(); |
68
|
|
|
} |
69
|
|
View Code Duplication |
} else { |
|
|
|
|
70
|
|
|
if ($this->webdriver->elementExists($this->theme->getUseBillingAddressForShipping(), WebDriver::BY_XPATH)) { |
71
|
|
|
$this->webdriver->byXpath($this->theme->getUseBillingAddressForShipping())->click(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (!$this->enterNewAddress && $this->webdriver->elementDisplayed($this->theme->getBillingAddressDropdownXpath(), WebDriver::BY_XPATH)) { |
76
|
|
|
// We're logged in and we have an address. |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function shouldProcess($xpath) |
84
|
|
|
{ |
85
|
|
|
if (!$xpath) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
return array_search($xpath, $this->bypass) === false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function sendData($xpath, $data) |
92
|
|
|
{ |
93
|
|
|
if ($this->shouldProcess($xpath)) { |
94
|
|
|
$this->testCase->byXpath($xpath)->clear(); |
95
|
|
|
$this->testCase->byXpath($xpath)->sendKeys($data); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function execute() |
100
|
|
|
{ |
101
|
|
|
if ($this->preExecute()) { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
$this->sendData($this->theme->getBillingEmailAddressXpath(), $this->customerIdentity->getEmailAddress()); |
105
|
|
|
$this->sendData($this->theme->getBillingFirstNameXpath(), $this->customerIdentity->getBillingFirstName()); |
106
|
|
|
$this->sendData($this->theme->getBillingLastNameXpath(), $this->customerIdentity->getBillingLastName()); |
107
|
|
|
$this->sendData($this->theme->getBillingCompanyXpath(), $this->customerIdentity->getBillingCompany()); |
108
|
|
|
$this->sendData($this->theme->getBillingAddressXpath(), $this->customerIdentity->getBillingAddress()); |
109
|
|
|
$this->sendData($this->theme->getBillingAddress2Xpath(), $this->customerIdentity->getBillingAddress2()); |
110
|
|
|
$this->sendData($this->theme->getBillingCityXpath(), $this->customerIdentity->getBillingCity()); |
111
|
|
|
|
112
|
|
|
$countryXpath = $this->theme->getBillingCountryIdXpath($this->customerIdentity->getBillingCountryId()); |
113
|
|
|
if ($this->shouldProcess($countryXpath)) { |
114
|
|
|
$this->testCase->byXpath($countryXpath)->click(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$regionXpath = $this->theme->getBillingRegionIdXpath($this->customerIdentity->getBillingRegionId()); |
118
|
|
|
if ($this->shouldProcess($regionXpath)) { |
119
|
|
|
$this->testCase->byXpath($regionXpath)->click(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->sendData($this->theme->getBillingPostCodeXpath(), $this->customerIdentity->getBillingPostCode()); |
123
|
|
|
|
124
|
|
|
$this->sendData($this->theme->getBillingTelephoneXpath(), $this->customerIdentity->getBillingTelephone()); |
125
|
|
|
$this->sendData($this->theme->getBillingFaxXpath(), $this->customerIdentity->getBillingFax()); |
126
|
|
|
|
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
View Code Duplication |
public function nextAction() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$this->testCase->byXpath($this->theme->getBillingContinueButtonXpath())->click(); |
134
|
|
|
|
135
|
|
|
$this->webdriver->wait()->until(WebDriverExpectedCondition::not(WebDriverExpectedCondition::visibilityOf($this->webdriver->byXpath($this->theme->getBillingContinueCompletedXpath())))); |
|
|
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.