[shorts] Como obter o valor das variáveis de ambiente do Windows com o Playwright e Java
21 de março de 2024
Framework Playwright, Pirâmide de Testes, QA, Testes Automatizados
![[shorts] Como obter o valor das variáveis de ambiente do Windows com o Playwright e Java](https://iagofrota.com.br/wp-content/uploads/2024/03/Como-obter-o-valor-das-variaveis-de-ambiente-do-Windows-com-o-Playwright-e-Java.png)
Uma pequena dica de como utilizar variáveis de ambiente no Playwright
Acessando Variáveis de Ambiente
Você pode acessar as variáveis de ambiente utilizando o método System.getenv()
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { // Acessando a variável de ambiente String playwrightUrl = System.getenv("PLAYWRIGHT_URL"); if (playwrightUrl != null) { System.out.println("Playwright URL: " + playwrightUrl); // Aqui você pode utilizar a URL com Playwright } else { System.out.println("A variável de ambiente PLAYWRIGHT_URL não está definida."); } } } |
Utilizando Variáveis de Ambiente
Supondo que você queira utilizar uma URL armazenada em uma variável de ambiente como parte do seu teste com Playwright, você poderia fazer algo assim:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import com.microsoft.playwright.*; public class PlaywrightTest { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true)); BrowserContext context = browser.newContext(); Page page = context.newPage(); // Acessando a URL a partir de uma variável de ambiente String url = System.getenv("PLAYWRIGHT_URL"); if (url != null) { page.navigate(url); // Adicione aqui mais lógica de teste } else { System.out.println("A variável de ambiente PLAYWRIGHT_URL não está definida."); } } } } |
Considerações
- As variáveis de ambiente são uma excelente maneira de gerenciar configurações que variam entre ambientes de desenvolvimento, teste e produção.
- Lembre-se de que as alterações em variáveis de ambiente podem exigir uma reinicialização do sua IDE ou do sistema operacional.