|
3 | 3 | */ |
4 | 4 | import { get } from 'lodash'; |
5 | 5 | import { |
6 | | - clearLocalStorage, |
7 | 6 | enablePageDialogAccept, |
8 | 7 | isOfflineMode, |
9 | 8 | setBrowserViewport, |
@@ -46,97 +45,48 @@ const OBSERVED_CONSOLE_MESSAGE_TYPES = { |
46 | 45 | }; |
47 | 46 |
|
48 | 47 | async function setupBrowser() { |
49 | | - await clearLocalStorage(); |
50 | 48 | await setBrowserViewport( 'large' ); |
51 | 49 | } |
52 | 50 |
|
53 | | -/** |
54 | | - * Navigates to the post listing screen and bulk-trashes any posts which exist. |
55 | | - * |
56 | | - * @return {Promise} Promise resolving once posts have been trashed. |
57 | | - */ |
58 | | -async function trashExistingPosts() { |
59 | | - await switchUserToAdmin(); |
60 | | - // Visit `/wp-admin/edit.php` so we can see a list of posts and delete them. |
61 | | - await visitAdminPage( 'edit.php' ); |
62 | | - |
63 | | - // If this selector doesn't exist there are no posts for us to delete. |
64 | | - const bulkSelector = await page.$( '#bulk-action-selector-top' ); |
65 | | - if ( ! bulkSelector ) { |
66 | | - return; |
67 | | - } |
68 | | - |
69 | | - // Select all posts. |
70 | | - await page.waitForSelector( '#cb-select-all-1' ); |
71 | | - await page.click( '#cb-select-all-1' ); |
72 | | - // Select the "bulk actions" > "trash" option. |
73 | | - await page.select( '#bulk-action-selector-top', 'trash' ); |
74 | | - // Submit the form to send all draft/scheduled/published posts to the trash. |
75 | | - await page.click( '#doaction' ); |
76 | | - await page.waitForXPath( |
77 | | - '//*[contains(@class, "updated notice")]/p[contains(text(), "moved to the Trash.")]' |
78 | | - ); |
79 | | - await switchUserToTest(); |
80 | | -} |
81 | | - |
82 | | -/** |
83 | | - * Navigates to the product listing screen and bulk-trashes any product which exist. |
84 | | - * |
85 | | - * @return {Promise} Promise resolving once products have been trashed. |
86 | | - */ |
87 | | -async function trashExistingProducts() { |
88 | | - await switchUserToAdmin(); |
89 | | - // Visit `/wp-admin/edit.php?post_type=product` so we can see a list of products and delete them. |
90 | | - await visitAdminPage( 'edit.php', 'post_type=product' ); |
91 | | - |
92 | | - // If this selector doesn't exist there are no products for us to delete. |
93 | | - const bulkSelector = await page.$( '#bulk-action-selector-top' ); |
94 | | - if ( ! bulkSelector ) { |
95 | | - return; |
96 | | - } |
97 | | - |
98 | | - // Select all products. |
99 | | - await page.waitForSelector( '#cb-select-all-1' ); |
100 | | - await page.click( '#cb-select-all-1' ); |
101 | | - // Select the "bulk actions" > "trash" option. |
102 | | - await page.select( '#bulk-action-selector-top', 'trash' ); |
103 | | - // Submit the form to send all draft/scheduled/published posts to the trash. |
104 | | - await page.click( '#doaction' ); |
105 | | - await page.waitForXPath( |
106 | | - '//*[contains(@class, "updated notice")]/p[contains(text(), "moved to the Trash.")]' |
107 | | - ); |
108 | | - await switchUserToTest(); |
109 | | -} |
110 | | - |
111 | 51 | /** |
112 | 52 | * Navigates to woocommerce import page and imports sample products. |
113 | 53 | * |
114 | 54 | * @return {Promise} Promise resolving once products have been imported. |
115 | 55 | */ |
116 | 56 | async function importSampleProducts() { |
117 | 57 | await switchUserToAdmin(); |
118 | | - // Visit Import Products page. |
119 | | - await visitAdminPage( |
120 | | - 'edit.php', |
121 | | - 'post_type=product&page=product_importer' |
122 | | - ); |
123 | | - await page.click( 'a.woocommerce-importer-toggle-advanced-options' ); |
124 | | - await page.focus( '#woocommerce-importer-file-url' ); |
125 | | - // local path for sample data that is included with woo. |
126 | | - await page.keyboard.type( |
127 | | - 'wp-content/plugins/woocommerce/sample-data/sample_products.csv' |
128 | | - ); |
129 | | - await page.click( '.wc-actions .button-next' ); |
130 | | - await page.waitForSelector( '.wc-importer-mapping-table' ); |
131 | | - await page.select( |
132 | | - '.wc-importer-mapping-table tr:nth-child(29) select', |
133 | | - '' |
| 58 | + // Visit `/wp-admin/edit.php?post_type=product` so we can see a list of products and decide if we should import them or not. |
| 59 | + await visitAdminPage( 'edit.php', 'post_type=product' ); |
| 60 | + const emptyState = await page.evaluate( () => |
| 61 | + window.find( 'Ready to start selling something awesome' ) |
134 | 62 | ); |
135 | | - await page.click( '.wc-actions .button-next' ); |
136 | | - await page.waitForXPath( |
137 | | - "//*[@class='woocommerce-importer-done' and contains(., 'Import complete! ')]" |
| 63 | + const noProduct = await page.evaluate( () => |
| 64 | + window.find( 'No products found' ) |
138 | 65 | ); |
139 | | - await switchUserToTest(); |
| 66 | + if ( emptyState || noProduct ) { |
| 67 | + // Visit Import Products page. |
| 68 | + await visitAdminPage( |
| 69 | + 'edit.php', |
| 70 | + 'post_type=product&page=product_importer' |
| 71 | + ); |
| 72 | + await page.click( 'a.woocommerce-importer-toggle-advanced-options' ); |
| 73 | + await page.focus( '#woocommerce-importer-file-url' ); |
| 74 | + // local path for sample data that is included with woo. |
| 75 | + await page.keyboard.type( |
| 76 | + 'wp-content/plugins/woocommerce/sample-data/sample_products.csv' |
| 77 | + ); |
| 78 | + await page.click( '.wc-actions .button-next' ); |
| 79 | + await page.waitForSelector( '.wc-importer-mapping-table' ); |
| 80 | + await page.select( |
| 81 | + '.wc-importer-mapping-table tr:nth-child(29) select', |
| 82 | + '' |
| 83 | + ); |
| 84 | + await page.click( '.wc-actions .button-next' ); |
| 85 | + await page.waitForXPath( |
| 86 | + "//*[@class='woocommerce-importer-done' and contains(., 'Import complete! ')]" |
| 87 | + ); |
| 88 | + await switchUserToTest(); |
| 89 | + } |
140 | 90 | } |
141 | 91 |
|
142 | 92 | /** |
@@ -256,8 +206,6 @@ beforeAll( async () => { |
256 | 206 | capturePageEventsForTearDown(); |
257 | 207 | enablePageDialogAccept(); |
258 | 208 | observeConsoleLogging(); |
259 | | - await trashExistingPosts(); |
260 | | - await trashExistingProducts(); |
261 | 209 | await setupBrowser(); |
262 | 210 | await importSampleProducts(); |
263 | 211 | } ); |
|
0 commit comments