<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Resource class in GitLab QA > 原文:[https://docs.gitlab.com/ee/development/testing_guide/end_to_end/resources.html](https://docs.gitlab.com/ee/development/testing_guide/end_to_end/resources.html) * [How to properly implement a resource class?](#how-to-properly-implement-a-resource-class) * [Define API implementation](#define-api-implementation) * [Resource attributes](#resource-attributes) * [Product data attributes](#product-data-attributes) * [Define an attribute based on an API response](#define-an-attribute-based-on-an-api-response) * [Creating resources in your tests](#creating-resources-in-your-tests) * [Where to ask for help?](#where-to-ask-for-help) # Resource class in GitLab QA[](#resource-class-in-gitlab-qa "Permalink") 資源主要是使用瀏覽器 UI 步驟創建的,但也可以通過 API 或 CLI 創建的. ## How to properly implement a resource class?[](#how-to-properly-implement-a-resource-class "Permalink") 所有資源類都應繼承自`Resource::Base` . 只有一種強制性方法可以實現以定義資源類. 這是`#fabricate!` 方法,用于通過瀏覽器 UI 構建資源. 請注意,在此方法中,您僅應使用[Page 對象](page_objects.html)與網頁進行交互. 這是一個假想的例子: ``` module QA module Resource class Shirt < Base attr_accessor :name def fabricate! Page::Dashboard::Index.perform do |dashboard_index| dashboard_index.go_to_new_shirt end Page::Shirt::New.perform do |shirt_new| shirt_new.set_name(name) shirt_new.create_shirt! end end end end end ``` ### Define API implementation[](#define-api-implementation "Permalink") 資源類還可以實現以下三種方法,以便能夠通過公共 GitLab API 創建資源: * `#api_get_path` :獲取現有資源的`GET`路徑. * `#api_post_path` :用于創建新資源的`POST`路徑. * `#api_post_body` :用于創建新資源的`POST`正文(作為 Ruby 哈希). > 請注意,許多 API 資源都是[分頁的](../../../api/README.html#pagination) . 如果找不到期望的結果,請檢查是否有超過一頁的結果. 讓我們使用`Shirt`資源類,并添加以下三個 API 方法: ``` module QA module Resource class Shirt < Base attr_accessor :name def fabricate! # ... same as before end def api_get_path "/shirt/#{name}" end def api_post_path "/shirts" end def api_post_body { name: name } end end end end ``` `Project`資源是瀏覽器 UI 和 API 實現的一個很好的真實示例. #### Resource attributes[](#resource-attributes "Permalink") 一個資源可能首先需要另一個資源. 例如,一個項目需要在其中創建一個組. 要定義資源屬性,可以將`attribute`方法與使用其他資源類的塊一起使用以構造資源. 這將允許從資源對象的方法訪問其他資源. 您通常會在`#fabricate!`使用它`#fabricate!` , `#api_get_path` , `#api_post_path` , `#api_post_body` . 讓我們使用`Shirt`資源類,并向其添加一個`project`屬性: ``` module QA module Resource class Shirt < Base attr_accessor :name attribute :project do Project.fabricate! do |resource| resource.name = 'project-to-create-a-shirt' end end def fabricate! project.visit! Page::Project::Show.perform do |project_show| project_show.go_to_new_shirt end Page::Shirt::New.perform do |shirt_new| shirt_new.set_name(name) shirt_new.create_shirt! end end def api_get_path "/project/#{project.path}/shirt/#{name}" end def api_post_path "/project/#{project.path}/shirts" end def api_post_body { name: name } end end end end ``` **請注意,所有屬性都是延遲構造的. 這意味著,如果您要首先構造特定的屬性,則即使不使用它,也需要首先調用 attribute 方法.** #### Product data attributes[](#product-data-attributes "Permalink") 創建后,您可能希望使用可在網頁或 API 響應中找到的屬性填充資源. 例如,創建項目后,您可能希望將其存儲庫 SSH URL 存儲為屬性. 同樣,我們可以將`attribute`方法與塊一起使用,使用頁面對象來檢索頁面上的數據. 讓我們以`Shirt`資源類`Shirt` ,并定義一個`:brand`屬性: ``` module QA module Resource class Shirt < Base attr_accessor :name attribute :project do Project.fabricate! do |resource| resource.name = 'project-to-create-a-shirt' end end # Attribute populated from the Browser UI (using the block) attribute :brand do Page::Shirt::Show.perform do |shirt_show| shirt_show.fetch_brand_from_page end end # ... same as before end end end ``` **再次注意,所有屬性都是延遲構造的. 這意味著,如果您`shirt.brand`另一頁面后再調用`shirt.brand` ,則由于我們不在預期的頁面上,因此將無法正確檢索數據.** 考慮一下: ``` shirt = QA::Resource::Shirt.fabricate! do |resource| resource.name = "GitLab QA" end shirt.project.visit! shirt.brand # => FAIL! ``` 上面的示例將失敗,因為現在我們在項目頁面上,試圖從襯衫頁面構造品牌數據,但是我們已經移至項目頁面. 有兩種解決方法,一種是我們可以在再次訪問該項目之前嘗試檢索該品牌: ``` shirt = QA::Resource::Shirt.fabricate! do |resource| resource.name = "GitLab QA" end shirt.brand # => OK! shirt.project.visit! shirt.brand # => OK! ``` The attribute will be stored in the instance therefore all the following calls will be fine, using the data previously constructed. If we think that this might be too brittle, we could eagerly construct the data right before ending fabrication: ``` module QA module Resource class Shirt < Base # ... same as before def fabricate! project.visit! Page::Project::Show.perform do |project_show| project_show.go_to_new_shirt end Page::Shirt::New.perform do |shirt_new| shirt_new.set_name(name) shirt_new.create_shirt! end populate(:brand) # Eagerly construct the data end end end end ``` `populate`方法將遍歷其參數并分別調用每個屬性. 這里`populate(:brand)`有像剛才一樣的效果`brand` . 使用填充方法使意圖更清晰. 這樣,將確保我們在創建襯衫后立即構造數據. 缺點是,即使我們不需要使用數據,也總是在構造資源時構造數據. 另外,我們可以在構建品牌數據之前確保在正確的頁面上: ``` module QA module Resource class Shirt < Base attr_accessor :name attribute :project do Project.fabricate! do |resource| resource.name = 'project-to-create-a-shirt' end end # Attribute populated from the Browser UI (using the block) attribute :brand do back_url = current_url visit! Page::Shirt::Show.perform do |shirt_show| shirt_show.fetch_brand_from_page end visit(back_url) end # ... same as before end end end ``` 這將確保在構建品牌之前,它在襯衫頁面上,并返回到上一頁以避免破壞狀態. #### Define an attribute based on an API response[](#define-an-attribute-based-on-an-api-response "Permalink") 有時,您想基于來自其`GET`或`POST`請求的 API 響應來定義資源屬性. 例如,如果通過 API 創建襯衫的返回 ``` { brand: 'a-brand-new-brand', style: 't-shirt', materials: [[:cotton, 80], [:polyamide, 20]] } ``` 您可能希望將`style` `main_fabric`在資源中,并在`main_fabric`屬性中獲取第一個`materials`項的第一個值. 讓我們以`Shirt`資源類`:main_fabric` ,并定義一個`:style`和`:main_fabric`屬性: ``` module QA module Resource class Shirt < Base # ... same as before # @style from the instance if present, # or fetched from the API response if present, # or a QA::Resource::Base::NoValueError is raised otherwise attribute :style # If @main_fabric is not present, # and if the API does not contain this field, this block will be # used to construct the value based on the API response, and # store the result in @main_fabric attribute :main_fabric do api_response.&dig(:materials, 0, 0) end # ... same as before end end end ``` **有關屬性優先級的說明:** * 資源實例變量具有最高優先級 * API 響應中的屬性優先于塊中的屬性(通常是來自瀏覽器用戶界面) * 沒有值的屬性將引發`QA::Resource::Base::NoValueError`錯誤 ## Creating resources in your tests[](#creating-resources-in-your-tests "Permalink") 要在測試中創建資源,可以調用`.fabricate!` 資源類上的方法. 請注意,如果資源類支持 API 構造,則默認情況下將使用該構造. 這是一個示例,由于`Shirt`資源類支持該方法,因此將在后臺使用 API??構造方法: ``` my_shirt = Resource::Shirt.fabricate! do |shirt| shirt.name = 'my-shirt' end expect(page).to have_text(my_shirt.name) # => "my-shirt" from the resource's instance variable expect(page).to have_text(my_shirt.brand) # => "a-brand-new-brand" from the API response expect(page).to have_text(my_shirt.style) # => "t-shirt" from the API response expect(page).to have_text(my_shirt.main_fabric) # => "cotton" from the API response via the block ``` 如果您明確希望使用瀏覽器 UI 的制作方法,則可以調用`.fabricate_via_browser_ui!` 方法: ``` my_shirt = Resource::Shirt.fabricate_via_browser_ui! do |shirt| shirt.name = 'my-shirt' end expect(page).to have_text(my_shirt.name) # => "my-shirt" from the resource's instance variable expect(page).to have_text(my_shirt.brand) # => the brand name fetched from the `Page::Shirt::Show` page via the block expect(page).to have_text(my_shirt.style) # => QA::Resource::Base::NoValueError will be raised because no API response nor a block is provided expect(page).to have_text(my_shirt.main_fabric) # => QA::Resource::Base::NoValueError will be raised because no API response and the block didn't provide a value (because it's also based on the API response) ``` 您還可以通過調用`.fabricate_via_api!`來顯式使用 API `.fabricate_via_api!` 方法: ``` my_shirt = Resource::Shirt.fabricate_via_api! do |shirt| shirt.name = 'my-shirt' end ``` 在這種情況下,結果將類似于調用`Resource::Shirt.fabricate!` . ## Where to ask for help?[](#where-to-ask-for-help "Permalink") 如果您需要更多信息,請在 Slack 上的`#quality`頻道(僅限內部,GitLab 團隊)上尋求幫助. 如果你不是一個團隊成員,你仍然需要幫助的貢獻,請打開 GitLab CE 問題追蹤的一個問題`~QA`標簽.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看